Understanding Primitive and Non-Primitive Data Types in JavaScript

Thumbnail

Written by: primepicks

Technology and Gadgets

Understanding Primitive and Non-Primitive Data Types in JavaScript

Primitive data types are the most basic building blocks of data in JavaScript. They are immutable (can’t be changed) and are stored directly in memory.

Primitive Data Types

// Datatypes

  
// number

  
let  num  =  20;

console.log(num);

console.log(typeof  num);

  
  
// string

  
let  str  =  "Hello World"

console.log(str);

console.log(typeof  str);

  

// Boolean

  
let  Do_you_follow_Viratkohli  =  true;

console.log(Do_you_follow_Viratkohli);

console.log(typeof  Do_you_follow_Viratkohli);

 

// undefined

  
let  followers;

console.log(followers);

  
// null


let  bank_balance  =  null;

console.log(bank_balance);

console.log(typeof  bank_balance); // the type of null is object.

  

// Bigint

  
let  bign  =  47684584584538945454n;

console.log(bign);

console.log(typeof  bign); //used for store big value.

Non-Primitive Data Types

//array

  
  

let  arr  = [20, 30, 40, 50, "hello"];

console.log(arr);

console.log(typeof  arr); //object

  

//object

//key : value

  
  

let  obj  = {

User_name : "Hakam",

Account_number : 45745674,

Balance : 500

}

console.log(obj);

console.log(typeof  obj); //object

  

// Function

  
  

let  fun  =  function(){

console.log("Hello coder Army");

return  10;

}

console.log(fun()); // For print the value of return

console.log(typeof  fun); // Function

  

//Type conversion

//string to number

  
  

let  account_balance  =  "500";

let  num1  =  Number(account_balance); // used for convert one type to another for example string to number

console.log(num1);

console.log(typeof  num1); // number

  

//number to string

  

let  str1  =  20;

console.log(String(str1));

console.log(typeof  str1); //number

  

//operators

  
  

console.log(20+30-10*2);

  

//divide multiply left to right

//add sub left to right

  

//modules gives reminder

  

console.log(20%3);

  

// ++ incriment operator , -- decrement operator

// sum++ post incriment , sum-- post decrement

// ++sum pre increment , --sum pre decrement

  

// ++ incriment operator

  

let  sum  =  20;

sum++;

console.log(sum); // 21

  

//-- decrement operator

  

let  sum1  =  20;

sum1--;

console.log(sum1); // 19

  

//Assignment operators

  

let  x  =  20;

x+=10; // x = x+10;

console.log(x);

  

let  y  =  20;

y-=10; // y = y-10;

console.log(y);

| Feature | Primitive | Non-Primitive | |----------------|-------------------------------|----------------------------| | Stored as | Value | Reference (memory address) | | Mutable? | Immutable | Mutable | | Types included | String, Number, Boolean, etc. | Object, Array, Function | | Comparison | Compared by value | Compared by reference |

Final Thoughts

Learning about data types helped me understand how JavaScript handles memory and how values behave when passed around in code. This knowledge is crucial for avoiding bugs and writing efficient programs.

Login To Add Comment

No comments yet.