Skip to content

Latest commit

 

History

History
153 lines (132 loc) · 4.69 KB

File metadata and controls

153 lines (132 loc) · 4.69 KB

Primitive vs Non-Primitive Data Types in JavaScript


| Feature | Primitive Data Types | Non-Primitive Data Types | |--------------------|-----------------------------=-----------------------|--------------------------------------------------------| | Examples | Number String Boolean undefined null BigInt Symbol| Object, Array, Function, Date, etc. | | Mutability | ❌ Immutable – cannot be changed | ✅ Mutable – can be modified | | Memory Location| Stored in Stack | Stored in Heap, reference is stored in Stack | | Copy Behavior | 📋 Copied by value | 🔗 Copied by reference | | Comparison | Compared by value | Compared by reference (memory address) | | Size in Memory | Fixed size, lightweight | Dynamic size, heavier | | Used for | Storing single, simple values | Storing collections, structured data | | Example Code | let x = 10; let y = x;x ≠ y after change | let a = [1,2]; let b = a;a == b (same reference)|


Primitive Data Types in JavaScript

In JavaScript, primitive data types are stored in the stack memory because they are immutable and have a fixed size.

1. String

  • Represents textual data.
  • Enclosed in single ('), double ("), or backticks (`).
  • Example:
    let name = "Vishnu";
    let greeting = 'Hello';
    let templateString = `Hello, ${name}`;

2. Number

  • Represents both integers and floating-point numbers.
  • Example:
    let age = 25;
    let price = 99.99;
    let infinityValue = Infinity;
    let notANumber = NaN;

3. BigInt

  • Used for large numbers beyond Number.MAX_SAFE_INTEGER.
  • Created by adding n to the end of an integer.
  • Example:
    let bigNumber = 9007199254740991n;

4. Boolean

  • Represents true or false values.
  • Example:
    let isLoggedIn = true;
    let hasPermission = false;

5. Undefined

  • A variable that has been declared but not assigned a value.
  • Represents an uninitialized variable or obsence of a value
  • Example:
    let user;
    console.log(user); // undefined

6. Null

  • Represents an intentional absence of value.
  • Represents the intentional absence of any object value
  • Example:
    let emptyValue = null;

7. Symbol

  • Used to create unique values.
  • Example:
    let uniqueKey = Symbol("id");

Notes

  • All primitive types are immutable (cannot be changed after creation).
  • typeof operator can be used to check data types.
  • null is an object due to a historical JavaScript bug.

Example:

console.log(typeof "Hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof null); // "object"
console.log(typeof undefined); // "undefined"
console.log(typeof Symbol("id")); // "symbol"

Non-Primitive Data Types in JavaScript

Non-primitive data types are stored in the heap, and their reference (memory address) is stored in the stack. They are mutable and used for storing collections or complex data.

1. Object

  • A collection of key-value pairs.
  • Example:
    const person = {
      name: "Vishnu",
      age: 22
    };

2. Array

  • Ordered collection of values (indexed).
  • Example:
    const numbers = [1, 2, 3];

3. Function

  • A block of reusable code.
  • Example:
    function greet() {
      console.log("Hello World");
    }

4. Date, RegExp, Map, Set, etc.

  • Built-in objects for special purposes.
  • Example:
    const today = new Date();
    const pattern = /abc/;
    const map = new Map();
    const set = new Set();

Notes

  • All non-primitive types are objects internally.
  • When you copy a non-primitive variable, you copy the reference, not the value.
  • Any changes via one reference affect all references.

Example:

let a = [1, 2, 3];
let b = a;
b.push(4);
console.log(a); // [1, 2, 3, 4] – a is affected too

Use Array.isArray() or typeof to distinguish between objects and arrays/functions:

console.log(typeof {}); // "object"
console.log(Array.isArray([])); // true
console.log(typeof function() {}); // "function"