Data Types
JavaScript is a dynamically typed language. This means variables are not bound to a specific data type; you can assign a number to a variable and later assign a string to that same variable.
1. The Two Categories
JavaScript data types are divided into two distinct categories: Primitives and Objects.

A. Primitives (The "Atoms")
Primitives are the simplest forms of data. They are immutable, meaning their value cannot be changed once created (though the variable holding them can be reassigned).
There are 7 primitive types:
Number: Floating-point numbers (there is no specific "integer" type in JS).
Examples:
42,3.14,-10.Special values:
Infinity,NaN(Not a Number).
String: A sequence of characters.
Examples:
"Hello",'World',`Backticks`.
Boolean: Logical entities.
Values:
true,false.
Undefined: A variable that has been declared but not assigned a value.
Value:
undefined.
Null: Represents the intentional absence of any object value.
Value:
null.
BigInt: For integers larger than the
Numbertype can safely represent (2^53 - 1).Example:
9007199254740991n.
Symbol: Unique and immutable identifiers, often used for object keys.
B. Objects (The "Collections")
Objects are collections of properties. Unlike primitives, objects are mutable.
Object:
{ name: "Alice", age: 25 }Array:
[1, 2, 3](Arrays are technically special objects).Function: Code blocks designed to perform tasks (also objects).
Date, RegExp, Map, Set: Built-in complex structures.
2. How JavaScript Treats Them: Value vs. Reference
The most important distinction between Primitives and Objects is how they are stored and copied.
Primitives: Passed by Value
When you assign a primitive to a variable, the variable stores the actual value.
Objects: Passed by Reference
When you assign an object to a variable, the variable stores a reference (memory address) pointing to where that object lives in memory, not the object itself.
Because x and y point to the exact same location in memory, changing one affects the other.
3. Type Conversion (Coercion)
Because JavaScript is dynamic, it often converts types automatically. This is called Implicit Coercion. You can also force a change using Explicit Conversion.
A. Explicit Conversion (Manual)
This is when you tell JS exactly what you want. It is safer and more readable.
B. Implicit Coercion (Automatic)
This happens when you apply operators to mismatched types.
1. String Concatenation wins over Math
If you use + and one side is a string, JS converts the other side to a string.
2. Math wins with other operators
If you use -, *, /, or %, JS tries to convert strings to numbers.
3. Equality: == vs ===
Double Equals (
==): Allows coercion. It converts types to match before comparing.5 == "5"istrue.0 == falseistrue.null == undefinedistrue.
Triple Equals (
===): Strict equality. No coercion allowed. Always use this.5 === "5"isfalse.
4. Truthy and Falsy Values
When a value is expected to be a Boolean (like in an if statement), JS converts it.
Falsy Values (There are only 6):
false0(and-0)""(empty string)nullundefinedNaN
Truthy Values:
Everything else.
"0"(non-empty string) -> true[](empty array) -> true{}(empty object) -> truefunction(){}-> true
5. The "Gotcha" of Typeof
You can check a variable's type using the typeof operator, but there are quirks.
Note: typeof null returning "object" is a historical bug in JavaScript that cannot be fixed without breaking existing code on the internet. null is actually a primitive.
Last updated