Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Primitive and Reference value in JavaScript

  • Difficulty Level : Easy
  • Last Updated : 08 Jan, 2021

In JavaScript, a variable may store two type of values, Primitive values or Reference values. This article will describe and help to compare both these type of values.

1. Primitive value: JavaScript provides six type of primitive values that includes Number, String, Boolean, Undefined, Symbol, and BigInt. The size of Primitive values are fixed, therefore JavaScript stores the primitive value in the call stack (Execution context).

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

When we access a primitive value, we manipulate the actual value stored in that variable. Thus, variables that are primitive are accessed by Value. When we assign a variable that stores a primitive value to another, the value stored in the variable is created and copied into the new variable.

Let us take an example to understand primitive value:



Primitive value

Javascript




let age = 30;
let age1 = age; // Pointing to age
  
console.log(`age = ${age}  age1 = ${age1}`);
  
age = 31; // Pointing to new address
  
console.log(`age = ${age}  age1 = ${age1}`);

Output:

 age = 30  age1 = 30
 age = 31  age1 = 30

2. Reference Value: JavaScript provides three types of Reference values that include Array, Object, and Function. The size of a reference value is dynamic therefore It is stored on Heap.

When we access a reference value, we manipulate it through reference, not through its actual value that is stored. Thus, variables that are reference values are accessed by reference. When we assign a reference value from one variable to another, the value stored in the variable is also copied into the location of the new variable but the difference is that the values stored in both variables now are the address of the actual object stored on the heap. As a result, both variables are referencing the same object, So we can manipulate the original object from both the variable.

Let us take an example to understand reference value:

Reference value

Javascript




let info = {
    Name :"Abc",
    Age :10
}
console.log(`Name : ${info.Name} Age : ${info.Age}`);
  
let info1 = info;
info1.Age = 14; // Change the Age of original object
console.log(`Name : ${info.Name} Age : ${info.Age}`);

Output: 

Name : Abc Age : 10
Name : Abc Age : 14



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!