Please enable Javascript to correctly display the contents on Dot Net Tricks!

Data Type

Posted On : 26 Mar 2015
Updated On : 26 Mar 2015
Total Views : 2704

JavaScript is a dynamic programming language. It means you don't have to declare the type of a variable at the time of declaration, it's type will be determine automatically at the time of program execution i.e at run-time. This is the reason you can have the same variable for different types.

Javascript Dynamic typing Exanple

var x = 42; // here, x is a Number
var x = "DOT NET TRICKS"; // here, x is a String
var x = true; // here, x is a Boolean
var x = {name:'John', age:34}; // here, x is an Object

JavaScript Data types

Typically, JavaScript has two types of data types : Primitive and Non-Primitive(Objects).

JavaScript Data Type

The typeof Operator

JavaScript typeof operator is used to find the type of a JavaScript variable.

typeof "John" // returns string 
typeof 3.14 // returns number
typeof Infinity; // returns number
typeof NaN; // returns number
typeof false // returns Boolean
typeof null // returns object and this is bug in ECMA script5

typeof [1,2,3,4] // returns object
typeof {name:'John', age:34} // returns object
typeof function(){} // returns function
typeof /^[0-9]$/I // returns object

var d = new Date();
typeof d // returns object
What do you think?

I hope, now you have better understanding of JavaScript data type. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.