Topic 10 of 37
Primitive Types
Overview
Primitives are the lowest level of data types in JavaScript. They are immutable (cannot be changed once created) and are passed by value. There are 7 primitive types: string, number, bigint, boolean, undefined, symbol, and null. Knowing these is essential for understanding how memory works.
Syntax
All these types store their values directly on the Call Stack.
The 7 Primitives
javascript
const str = "Hello"; // String
const num = 42; // Number (Float 64-bit)
const big = 9007199254n; // BigInt
const bool = true; // Boolean
const undef = undefined; // Undefined
const sym = Symbol("id"); // Symbol
const empty = null; // NullCommon Pitfalls
- Thinking primitives have methods. They don't! JS temporarily wraps them in Object wrappers (like String or Number) to use methods, then destroys the wrapper (Auto-boxing).
Interview Tips
- Know the quirks: typeof null === 'object' is a famous historical bug in JS.
Real-World Example
Using Symbols to create unique property keys that won't clash.
example
javascript
const SECRET_KEY = Symbol('secret');
const user = {
name: "Alice",
[SECRET_KEY]: "SuperPassword123"
};
console.log(Object.keys(user)); // ['name'] - the symbol is hidden from normal iteration