π Two Main Categories
JavaScript divides data types into two broad groups:
1οΈβ£ Primitive types (simple, immutable values)
2οΈβ£ Non-primitive types (objects, complex structures)
Letβs go through them.
πΉ Primitive Data Types
These are the most basic types β they hold a single value and are immutable (cannot be changed).
β
Number
For any numeric value, including decimals.
let age = 25;
let price = 99.99;
β String
For text, wrapped in single ' or double " quotes.
let name = "Elayaraj";
let message = 'Hello, world!';
β Boolean
True or false β used for logic and conditions.
let isActive = true;
let hasAccess = false;
β Undefined
A variable that has been declared but not assigned a value.
let result;
console.log(result); // undefined
β
Null
An intentional empty value.
let user = null;
β Symbol
A unique and immutable value, often used as an object key (advanced use).
let id = Symbol("uniqueId");
β BigInt
For very large integers beyond Number limits.
let bigNumber = 123456789012345678901234567890n;
πΉ Non-Primitive Data Types
These hold collections of values and are mutable.
β Object
A collection of key-value pairs.
let person = {
name: "Elayaraj",
age: 25
};
β Array
An ordered list of values (technically an object).
let fruits = ["apple", "banana", "cherry"];
β Function
A reusable block of code (also a type of object).
function greet() {
console.log("Hello!");
}
π§ Why Does This Matter?
Understanding data types helps you:
- Write bug-free code (no type errors)
- Use correct operations (e.g., adding numbers vs. concatenating strings)
- Master type conversions (e.g., turning strings into numbers)
For example:
let x = "5" + 2; // "52" (string concatenation)
let y = "5" - 2; // 3 (number subtraction)
π Type Checking
You can check a valueβs type using:
typeof 42; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof null; // "object" (this is a known quirk!)
typeof []; // "object"
typeof function() {}; // "function"
Top comments (0)