primitive types js code example

Example 1: js data types

//There are 7 data types in JS 
//They're split in two categories - (Primitives and Complex Types)

//Primives
string, number, boolean, null, undefined

//Complex types
Object, Function

Example 2: primitive and non primitive data types in javascript

//primitive = {STRING, NUMBER, BOOLEAN, SYMBBOL, UNDEFIEND, NULL}
//non-primitive = {ARRAY, OBJECT, FUNCTION}

//primitive is always copied by VALUE
var a = 1;
var b = a;
    //console.log(a , b) =  1 , 1
a = 3;
console.log(a) //3
console.log(b) // still 1 and not 3   (always copied by value only)


//non-primitive is always copied by REFERENCE
var x = {name : "Jscript"};
var y = x;
  //console.log(x , y)     TWICE =  Object { name: "Jscript" }
x.name = "Js";
console.log(x) //Js
console.log(y) //Js  {copied by reference} like pointers in C lang

Example 3: primitive and non primitive data types in javascript

Primitve vs Non Primitive

Example 4: data types in javascript

1, 1.0 // Number
'String', "String"
true, false // Boolean
{id: 1;} // Object
[1, 2, 3] // Array