13. BASICS | Memory Address in JavaScript
This article will talk about memory addresses of primitive and non-primitive data types.
Memory Address of Primitive Data Types
// strings
const firstName1 = "John";
const firstName2 = "John";
const firstName3 = "Jo" + "hn"; // John created in a different way
const lastName = "Doe";
const emptyString = '';
// numbers
const age = 25;
const diff = 50 - 25;
const birthYear = 2000;
// booleans
const isStudent = true;
const isGraduate = false;
// null and undefined
const middleName = null;
const middleName2 = undefined;
// symbol
const uniqueId1 = Symbol('id');
const uniqueId2 = Symbol('id'); // uniqueId1 and uniqueId2 are different
console.log(firstName1 === firstName2); // true
console.log(firstName1 === firstName3); // true
Important Points:-
Same string value created in the same way (firstName1 and firstName2) always point to the same memory address. Same string value created in different ways (firstName1 and firstName3) always point to different memory address.
Same number value created in different way (age and diff) always point to the same memory address.
Booleans,
nullandundefinedalways point to the same memory address.Symbols always point to different memory address, as every symbol is unique.
You can see memory addresses in the Memory tab of dev tools by taking the snapshot.
Why primitive data types are called 'call by value'?
Because when we compare two primitive data types, we always compare them by their values not by their memory addresses.
Memory Address of Non - Primitive Data Types
// object literals
const obj1 = {};
const obj2 = {};
console.log(obj1 === obj2); // false
const userDetails = {
firstName: 'Shubham',
lastName: 'Agrawal',
pata: {
city: 'Noida',
pincode: 281004,
state: 'UP'
moreDetails: {
population: 568429873,
area: '400 sq. km.'
}
}
}
// arrays
const arr1 = [];
const arr2 = [];
console.log(arr1 === arr2); // false
const userDetails = [
'Shubham',
'Agrawal',
25,
{
city: 'Noida',
pincode: 281004,
state: 'UP'
}
];
Why non - primitive data types are called 'call by reference'?
Because when we compare two non - primitive data types, we always compare them by their memory addresses not by their values .
Happy reading!

