9

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.

4
  • 7
    Primitive have value, non-primitives have reference, this is the main difference Commented Oct 22, 2015 at 5:14
  • Also, primitives are not initially instantiated as objects, whereas non-primitives are (I think this is the reason for value vs. reference as mentioned above). Commented Oct 22, 2015 at 5:28
  • Types don't have anything to do with variables in JS, they are not "declared". They only belong to the values. Commented Oct 22, 2015 at 6:14
  • 2
    Possible duplicate of What is the main difference between primitive type and wrapper class? Commented Oct 22, 2015 at 6:38

7 Answers 7

4
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
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

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.

Comments

1

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

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

Comments

1

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.

Comments

0

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)

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.