javascriptooptypesabstract-data-type

Difference between Primitive and non-primitive datatypes in JavaScript


I am not able to understand exactly what is difference between primitive and non primitive data types in JavaScript even it is declared using same name i.e var.


Solution

  • Data Types (JavaScript):
    
    Primary Data Types
    The primary (primitive) data types are:
    String, Number, Boolean
    
    Composite Data Types
    The composite (reference) data types are:
    Object, Array
    
    Special Data Types
    The special data types are:
    Null, Undefined
    

    Click here for details:

      var test1 = 1;
      var test2 = "Something";
      var test3 = true;
      var test4 = {};
      var test5 = new Array();
      var test6 = new Date();
      var test7;
      var test8 = null;
    
      alert(typeof (test1)); //number
      alert(typeof (test2)); //string
      alert(typeof (test3)); //boolean
      alert(typeof (test4)); //object
      alert(typeof (test5)); //object
      alert(typeof (test6)); //object
      alert(typeof (test7)); //undefined
      alert(typeof (test8)); //object