Javascript Datatype

JavaScript has basically two type of datatypes
1) Primitive Datatype
2) Composite Datatype

1) Primitive Datatype:- These type of datatype is immutable and it do not have properties. When you make a copy, it will be a real copy.

Example:- when you reassign a new value to b, the value of b changes, but not of a.


 <script>
 const a = "John";
 let b = a   // this is the copy
 b = "Rom";
 document.write(b) // Rom
 document.write(a) // John
 </script>

Try it Yourself

1) String:- It is basically used to store string value and string value represents through single quote or double quotes.


 var name="John";
 console.log(typeof name);  //string

2) Number:- It is used to store number value.


 var age=35;
 console.log(typeof age);  //number

3) undefined:- A variable that has not value is undefined.


  var name;
  console.log(typeof name); // undefined 

4) Null:- It has null value.


 var nothing = null;
 console.log(nothing);

5) Boolean:- this datatype variable has value true or false.


 var is_loggedin=true;
 console.log(is_loggedin);  //true

 var not_loggedin=false;
 console.log(not_loggedin);  //false

6) Symbol:- It came into ECMAScript6 (ES6). It represents a unique identifier.

2) Composite Datatype:- Object, Array and methods are compositive datatypes. Technically, arrays are also objects, so they behave in the same way. These values are actually stored just once when instantiated, and assigning a variable just creates a pointer (reference) to that value.

Object Example:-


 var users={
      name:"John",
      age:35
        }

 console.log(typeof users) //Object

Array Example:-


 var users=[{
       name:"John",
       age:35
       }]

 console.log(typeof users) //Object

Example:- Now, if we make a copy b = a , and change some nested value in b, it actually changes a’s nested value as well, since a and b actually point to the same thing.


 <script>
 const a = {name:'John'}
 const b=a;
 b.name='Rom';
 document.write(a.name);  //Output:- Rom
 document.write(b.name);  //Output:- Rom
 </script>
 

Try it Yourself