2015-03-30

Step By Step JavaScript Data Type

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

  1. <script>
  2. var x = 42; // here, x is a Number
  3. var x = "DOT NET TRICKS"; // here, x is a String
  4. var x = true; // here, x is a Boolean
  5. var x = {name:'John', age:34}; // here, x is an Object
  6. </script>

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.
  1. <script>
  2. typeof "John" // returns string
  3. typeof 3.14 // returns number
  4. typeof Infinity; // returns number
  5. typeof NaN; // returns number
  6. typeof false // returns Boolean
  7. typeof null // returns object and this is bug in ECMA script5
  8.  
  9. typeof [1,2,3,4] // returns object
  10. typeof {name:'John', age:34} // returns object
  11. typeof function(){} // returns function
  12. typeof /^[0-9]$/I // returns object
  13.  
  14. var d = new Date();
  15. typeof d // returns object
  16. </script>

1 comment: