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. JavaScript ES6 supports 7 Primitive data types. But till JS ES5, we had only 5 Primitive data types.
As JavaScript and all scripting languages are loosely typed, there is no typecast in javascript. JS supports dynamic typing . We can create any type of data using a single variable.
var
means a variable which can store any type of data. Data type of variable is not declared.
Declaring var
means creating a new variable in memory with variable name after white-space. Assignment Operator (=
) means assigning value to variable declared. We can also use const
and let
to declared variables in ES6.
Primitive data types
Primitive data types are the basic or common data types in javascript. 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.
var, const and let are used to declare primitive data types in javascript.
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 are data types based on primitive. Like Array, Object and Functions. 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.
var, const and let is also used to declare reference data types. For Function and Class, we can also use function and 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.