JavaScript Data Types
Written By: Avinash Malhotra
Updated on
Data Types in JavaScript
Data Types in JavaScript define how data is stored and manipulated in your programs. JavaScript provides two main categories of data types: primitive types that store simple values (String, Number, Boolean, undefined, null, BigInt, Symbol) and reference types that store complex data (Object, Array, Function).
JavaScript data types are divided into two categories: Primitive Types and Reference Types. Primitive types are immutable and stored directly in memory by value, making them efficient for simple data. Modern JavaScript (ES6+) supports 7 primitive types, an expansion from the 5 types available in ES5. Reference types store addresses (references) to values in memory, making them ideal for complex data structures and functions.
As JavaScript and all scripting languages are loosely typed, there is no typecast in javascript. JS supports dynamic typing. Means We can create any type of data using a single variable.
Declaring variable means creating a new variable in memory with variable name and value. Assignment Operator (=) means assigning value to variable declared..
Types of Data Types in JavaScript
Primitive data types
Primitive data types are the fundamental building blocks of JavaScript. They represent simple values that are immutable (cannot be changed). All primitive types except null and undefined have corresponding object wrapper types (like String, Number) that provide useful methods and properties for working with these values. JavaScript originally had five primitive types (string, number, boolean, undefined, null), but ES6 added BigInt and Symbol, bringing the total to seven.
A key characteristic of primitive data types is that they are passed by value, not by reference. This means when you assign a primitive value to a new variable or pass it to a function, a copy of the value is created. For example, if x = 3 and y = x, changing y to 5 won't affect the value of x.
In modern JavaScript, we declare variables using const (for values that won't be reassigned), let (for values that will change), and rarely var (older syntax, generally avoided in modern code). The choice of declaration doesn't affect the data type itself but rather how the variable can be used.
How to declare Primitive Data Types
| Primitive Data Type | Meaning |
|---|---|
| const x="HELLO" | string |
| const x="3" | string |
| const x='3' | string |
| let x; | undefined |
| const x=undefined; | undefined |
| const x=null; | null |
| const x=3; | number |
| const x=3.5 | number |
| const x=2n | BigInt |
| const x=Symbol('a') | Symbol |
| const x=true | Boolean |
| const x=false; | Boolean |
Strings
Anything written in single, double quotes or backtick is a string in javascript. Strings are used to store name, email, city name, password etc in javascript. JavaScript Strings.
const name="js string";
Numbers
JavaScript Numbers are used to perform Arithmetic Operations (+,-,*,/,%). Numbers are written without quotes. JavaScript Numbers.
const num=20;
Boolean
JavaScript Boolean are true and false. Booleans are used in conditions, comparison etc.
const t=true;
const f=false;
Undefined
JavaScript Undefined means any variable whose value is not assigned yet. Anything variable whose value is not assigned is undefined.
let u;
const t=undefined;
Null
JavaScript null is a special object with empty value. null is used where value is defined, but still it is not there. It is also used in exception handling.
const u=null;
BigInt
JavaScript BigInt introduced in ES6 are used to extend numbers range. By-default JS Numbers supports 53 bit. The maximum safe integer in javascript is 253-1, i.e. 9007199254740991. After that numbers jumps to next or previous even number. BigInt are used to works on numbers greater than maximum safe integer, i.e. 9007199254740991.
const u=2n;
const v=9007199254740992n;
Symbol
JavaScript Symbol are primitives to get unique value. A Symbol value is always unique.
const a=Symbol("a");
const b=Symbol("a");
console.log(a===b); // false
Reference Data Type in JAVASCRIPT
Reference data types like Array, Object and Functions are advance data types, based which pass by reference, not value. They are stored on heap memory. Everything is JavaScript is either a primitive datatype or Object. Even Arrays and Functions are objects, but they are build-in objects.
All reference datatypes are Constructor Objects, that means they are created using new keyword. new means, a new instance of Function Object or class.
| Reference Data Type | Meaning |
|---|---|
const month=[ "Jan", "Feb", "Mar" ]; |
Array |
const user={ name : "ABC", age : 22 }; |
Object |
function sum(x,y){ return x+y;} |
Function Declaration |
const x=function(x,y){ return x+y;}; |
Function Expression |
const x=new Date(); |
Date |
const x=/^[0-9]{6}$/; |
RegExp |
class Car{} |
Class Declaration |
const Car=class{} |
Class Expression |
typeof Operator
typeof operator in javascript is used to check data type of a variable. It can return string, number, boolean and undefined. For reference type and null, typeof operator will return object.
let x; // undefined
const y=9; // number
const z="Tech Altum"; // string
typeof(x) and typeof x will return undefined,
typeof(y) and typeof y will return number,
typeof(z) and typeof z will return string.
| Given Expression | Check Data Type |
|---|---|
let x; |
|
const x=undefined; |
|
const x=null; |
|
const x=3; |
|
const x=3.5; |
|
const x="3"; |
|
const x='3'; |
|
const x="HELLO"; |
|
const x=true; |
|
const x=false; |
|
const x=function(a,b){ return a+b}; |
|
function add(a,b){ return a+b} |
|
const month=["jan","feb","mar"]; |
|
const user={ name:"abc", age:22}; |
Video
- typeof operator can check datatype of strings, numbers, undefined, boolean, bigint, symbols and functions.
- For Arrays, & Objects, typeof operator will return object.
- typeof(null), array and object will return Object.