JavaScript Data Types
Written By: Avinash Malhotra
Updated on
Data Types in JavaScript
Data Types in JavaScript means the type of data stored in a variable, like String, Number, boolean, undefined, null, function, Array, Object etc all are commonly used data types in javascript.
Data Types are categorized as Primitive or Reference. Primitive are basic data types which are stored by value. JavaScript ES6 onwards supports 7 Primitive data types. But till JS ES5, we had only 5 Primitive data types. Reference Data Types which store value by reference, not value. For Example, Function, Array and Objects.
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 basic or common data types in javascript. All primitive data types except null and undefined have their corresponding Object wrapper types which provided properties and methods, exp String, Number etc. Earlier JS has Five Primitive datatypes, string, numbers, boolean, undefined and null. ES6 introduced BigInt and Symbols. So now there are Seven Primitive datatypes in JavaScript.
Primitive data types are passed by value, not reference. Means if x is 3 and y is x, changing y to 5 will not change value of x.
var, const and let are used to declare data types in javascript.
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.