What is Value Types and Reference Types in JavaScript

Yashraj - Jun 16 - - Dev Community

Two fundamental categories that every developer should understand are value types and reference types. Value type is basically it will not changeable after something is assigned to variable and reference type is what we can change after declaration.

In JavaScript objects are reference type and all other data types are value types. It is important to know the difference between this to datatypes which will help you avoid common pitfalls and write more efficient code.

Value type example
let a = 10;
let b = a; // b is now 10
b = 20; // changing b does not affect a
console.log(a); // 10

Reference type example
let obj = { name: 'John' };
obj.name = 'Doe'; // the original object is modified
console.log(obj.name); // 'Doe'

Learn more...

. . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player