Primitive Types
Overview
Primitive types are the most basic data blocks in JavaScript. They represent single, simple values.
There are 7 primitive types in modern JS: String, Number, Boolean, Undefined, Null, Symbol, and BigInt.
Syntax
All primitives are immutable (they cannot be altered). When you reassign a primitive variable, you aren't changing the old value, you are replacing it with a completely new one.
const name = "Priya"; // String
const age = 22; // Number (No floats/ints, just Number)
const isStudent = true; // Boolean
let score; // Undefined (Declared, no value)
const car = null; // Null (Intentional empty value)
const uniqueId = Symbol("id");// Symbol (Guaranteed unique identifier)
const hugeNum = 90071992n; // BigInt (For massive integers)Common Pitfalls
- Thinking
nullandundefinedare the exact same thing.undefinedmeans JS hasn't assigned a value yet.nullmeans YOU explicitly set it to be empty.
Interview Questions
Primitive types are passed by value. When you assign a primitive variable to another, a strict copy of the value is created in memory.
Undefined means a variable has been declared but not assigned a value. Null is an assignment value representing 'no value' or an empty object reference.
Real-World Example
When fetching data from an API, a user's avatar might be null if they haven't uploaded one. If the API forgot to send the avatar field entirely, it would be undefined.
const userFromApi = {
name: "Kartik",
avatar: null // Explicitly empty
};Check Your Knowledge
Test your understanding of Primitive Types with these quick questions.