Primitive and Reference value in JavaScript
Last Updated :
12 Jan, 2023
In JavaScript, a variable may store two types of values, Primitive values or Reference values. This article will describe and help to compare both these types of values.
Primitive value: JavaScript provides six types of primitive values that include 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).
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.
Example: 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
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 variables.
Example: 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
Similar Reads
Pass by Value and Pass by Reference in Javascript JavaScript handles variables in different ways when passing them to functions. Variables in JavaScript can either be passed by value or passed by reference, depending on the type of data they hold. "Pass by Value" in JavaScriptWhen a variable is passed by value, a copy of the actual value is passed
3 min read
Reference and Copy Variables in JavaScript In this article, we will talk about pass-by-value and pass-by-reference in JavaScript. JavaScript always passes by value, but in an array or object, the value is a reference to it, so you can 'change' the data. JavaScript has 5 primitive data types that are passed by value, they are Boolean, NULL, u
5 min read
Call by Value Vs Call by Reference in JavaScript Call by Value: In this method, values of actual parameters are copied to the functionâs formal parameters, and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. Suppose there is a varia
3 min read
JavaScript Math Reference JavaScript Math object is used to perform mathematical operations on numbers. Math is an inbuilt object that works with numbers types but it does not work with BigInt.Example: Below example will give you a brief idea of JavaScript math objects.javascript// Return PI value(3.141592653589793) console.
3 min read
Primitive and Non-primitive data-types in JavaScript Variables hold values, and every value has a specific data type that defines the kind of information it holds. These data types are broadly categorized into two groups: Primitive Data Types and Non-Primitive Data Types. Let us discuss it one by one.1. Primitive Data Types Primitive data types are th
4 min read