Difference between Primitive and non-primitive datatypes in JavaScript

Durga Prasad picture Durga Prasad · Oct 22, 2015 · Viewed 17.8k times · Source

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.

Answer

Sudipta Kumar Maiti picture Sudipta Kumar Maiti · Oct 22, 2015
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