Variables hold values, and every value has a specific data type that defines the kind of information it holds. These data types are broadly categorized into two groups: Primitive Data Types and Non-Primitive Data Types.
Data types are categorized into Primitive and Non-Primitive (Reference) types in JavaScript.
Primitive
When you assign a primitive value:
- It is stored directly in memory
- Copying the value creates a new independent value
They are immutable, meaning you cannot change the value itself—only replace it.
JavaScript has 7 primitive types:
1. Number
All numbers (integer or floating point).
let x = 10;
let y = 11.5;
2. String
Text data.
let name = "Hello World!";
3. Boolean
True or False.
let isOnline = true;
4. Null
Intentional empty value.
let data = null;
5. Undefined
A variable declared but not assigned a value.
let a; // undefined
6. Symbol
Symbol data type is used to create objects that will always be unique. These objects can be created using Symbol constructor.
let sym = Symbol("Hello")
console.log(typeof(sym)); // Symbol
console.log(sym); // Hello
7. BigInt
For very large integers beyond Number’s limit.
let big = 12345678901234567890n;
Non-Primitive (Reference)
This data type is mutable, which means you can change/modify it. Allows you to modify existing values without creating new ones.
Non-primitive values are stored by reference. When you assign an object to another variable, JavaScript copies the reference, not the value.
These are objects—more complex data structures. The main non-primitive types:
- Object
- Array (actually a special type of object)
- Function (also an object)
- Date
- Map, Set, etc. (still objects)
let obj1 = { age: 27 };
let obj2 = obj1;
obj2.age = 30;
console.log(obj1.age); // 30 (obj1 also changed)
Why? Because both variables point to the same memory address.
Top comments (0)