Skip to main content

Command Palette

Search for a command to run...

13. BASICS | Memory Address in JavaScript

This article will talk about memory addresses of primitive and non-primitive data types.

Updated
2 min readView as Markdown
S
Full-Stack Developer with 4 years' experience, specializing in backend development. Skilled in JavaScript, React, Python, Databases, and AWS. Known for building scalable web apps, leading teams, and maintaining strong client communication. Upskilling in Generative AI.

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:-

  1. 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.

  2. Same number value created in different way (age and diff) always point to the same memory address.

  3. Booleans, null and undefined always point to the same memory address.

  4. 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!