Javascript Datatypes

datatypes in javascript

Javascript is a loosely typed, prototype based scripting language. We can create any type of data using a single variable var.
"var" means a variable which can store any type of data. Data type of variable is not declared.

Two Popular Datatypes in JavaScript

Primitive Type in JAVASCRIPT

Primitive are very basic or common data types in javascript. Like string, numbers, boolean, undefined and null.

Primitive Data Type Meaning
var x; undefined
var x=undefined; undefined
var x=null; null type data
var x=3; Data Type is number.
var x=3.5 Data Type is number with decimal
var x="3" Data Type is string
var x='3' Data Type is string
var x="HELLO" Data Type is string
var x=true Boolean data type
var x=false; Boolean data type

Reference Type in JAVASCRIPT

Reference are datatypes based on primitive. Like Array, Object, Functions, Date and regex.

Reference Data Type Meaning
var x=[ "Jan", "Feb", "Mar" ] Array
var x={ "name" : "ABC", "age" : "22", "gender" : "male" } Object
var x=function(x,y){ return x+y;} Function
function sum(x,y){ return x+y;} Function
var x=new Date(); Date
var x=/^[0-9]{6}$/ Regex

typeof Operator

typeof operator 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.

var x;                 // undefined
var y=9;               // number
var 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
var x;
var x=undefined;
var x=null;
var x=3;
var x=3.5;
var x="3";
var x='3';
var x="HELLO";
var x=true;
var x=false;
var x=function(a,b){ return a+b};
function add(a,b){ return a+b}
  • typeof operator can check datatype of strings, numbers, undefined, boolean and function. For Arrays, Object and null, typeof operator will return object.
  • typeof(null) will return Object.
  • null==undefined will return true, but null===undefined will return false, as data types are different.