Durga Prasad
Durga Prasad

Reputation: 141

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.

Upvotes: 9

Views: 22834

Answers (7)

Aamod Tiwari
Aamod Tiwari

Reputation: 1466

The latest ECMAScript standard defines eight data types:

  1. String
  2. Boolean
  3. Number
  4. BigInt
  5. Null
  6. Undefined
  7. Symbol
  8. Object

Note :- Arrays do not belong to this list because they are objects as well. This is a common confusion among developers who assume that arrays are a special data type in Javascript.

Now bifurcate the data type

There are two types of data types in JavaScript:

  1. Primitive values (String, Boolean, Number, BigInt, Null, Undefined, Symbol )

  2. Non-primitive values is also called object references (Object,Array)

Upvotes: 0

saggy
saggy

Reputation: 47

It's simple, just remember primitive data types means the data type which contain a single value, like 1, 'test', true,

Non-primitive data-types means the data type which contain multiple values or complex data called non-primitive like an object etc.

Upvotes: -1

Ritesh kumar
Ritesh kumar

Reputation: 31

Primitive datatype are type provided by the programming language as the basic building block. like: number,string,null,undefined,Boolean.   Primitive datatype get the memory allocation at compile time.

Derived datatype are type which are derived from the existing primitive datatype bundled together like: array,object etc. Derived datatype just get the reference to that memory allocation where value are saved.

Upvotes: 1

suresh narasimman
suresh narasimman

Reputation: 75

Primitive Datatypes are 1. Number 2. String 3. Boolean 4. Undefined 5. Null

Non-Primitive Datatypes are 1.Object 2.Array 3.RegExp

Upvotes: 1

Islam Sayed
Islam Sayed

Reputation: 56

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

Primitive Data types in ES6 are: Boolean Null Undefined Number String Symbol

The other data type which is not a primitive one is Object

Primitive data types are immutable values.

Upvotes: 1

Fazle Rabbi Tanjil
Fazle Rabbi Tanjil

Reputation: 236

Javascript has five primitive data types: 1. number 2. string 3. boolean 4. undefined 5. null

Anything that doesn’t belong to any of these five primitive types is considered an object.

First 3 data types has a corresponding object constructor. For example

var word = "something";

And then as an object:

 var word = new String("something");

For object constructor notice the new keyword. It creates object reference.

Another thing to notice that

var greeting = "something";
var word = new String("something");
greeting == word ----> True as their value is same
greeting === word -----> False because there value same but type is different .

As for var keyword being same for all the cases,remember that Javascript is dynamically typed language. That means it resolves data type checking during runtime rather than compile time(like Java, C++ ).

This makes javascript extremely powerful. Though this unique feature has drawback too. Please co through this wikipedia for details: https://en.wikipedia.org/wiki/Type_system#Static_and_dynamic_type_checking_in_practice

Hope this helps.

Upvotes: 4

Sudipta Kumar Maiti
Sudipta Kumar Maiti

Reputation: 1709

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

Upvotes: 4

Related Questions