Data Types and the Equality Matrix in JavaScript
In the previous lesson, you saw how variables store values. But what kinds of values can JavaScript hold?
Unlike languages like Java or C++ where you explicitly state int age or String name, JavaScript is dynamically typed. The data type is tied to the value itself, not the variable name.
By the end of this lesson, you will understand:
- The 7 Primitive Data Types in JavaScript
- How to check types using the
typeofoperator (and its famous historical quirk!) - The critical difference between Strict Equality (
===) and Loose Equality (==) - The 6 official Falsy values in JavaScript
1. The 7 Primitive Data Types 🧱
In JavaScript, a primitive is a single, fundamental value that is not an object and has no methods. There are exactly 7 primitives:
// 1. String (Text)const name = "Aarav";// 2. Number (Integers and Decimals combined)const age = 21;const price = 99.99;// 3. Boolean (True or False)const isLoggedIn = true;// 4. Undefined (Variable declared, but no value assigned yet)let userCity;console.log(userCity); // undefined// 5. Null (Intentional absence of any value)const profilePhoto = null; // Deliberately set to empty// 6. BigInt (Large integers beyond 2^53 - 1)const hugeNumber = 9007199254740991n;// 7. Symbol (Guaranteed unique and immutable identifier)const id = Symbol("userId");2. Inspecting Types with typeof 🔍
JavaScript includes a built-in operator called typeof that returns a string describing a value's type:
console.log(typeof "Hello"); // "string"console.log(typeof 42); // "number"console.log(typeof true); // "boolean"console.log(typeof undefined); // "undefined"console.log(typeof 100n); // "bigint"The Famous JavaScript Bug: typeof null 🐛
Try running this in your console:
console.log(typeof null); // "object" (Wait, what?!)null is definitively a primitive, NOT an object. This is a famous bug from the very first version of JavaScript in 1995. Fixing it now would break millions of legacy websites across the world, so it remains in the language spec forever. Just remember: null is a primitive!
3. Strict Equality (===) vs. Loose Equality (==) ⚖️
This is by far the most famous stumbling block for JavaScript beginners:
console.log("5" === 5); // falseconsole.log("5" == 5); // true (Wait, why?!)Strict Equality (===): Triple Equals 🛡️
Strict equality checks both the Type AND the Value:
- Are they the same type? (string vs. number →
false!) - No type conversion happens. It is 100% predictable, safe, and recommended for all modern code.
Loose Equality (==): Double Equals ⚠️
Loose equality performs Implicit Type Coercion:
- If the types are different, JavaScript attempts to forcefully convert one type into another before comparing.
- It converts the string
"5"into the number5, and then evaluates5 == 5, returningtrue!
4. Truthy vs. Falsy Values in JavaScript 🚦
When JavaScript expects a boolean (for example, inside an if condition), any value can be coerced into either true (truthy) or false (falsy).
You don't need to memorize hundreds of truthy values. Instead, simply remember that there are only 6 falsy values in the entire JavaScript language:
┌────────────────────────────────────────────────────────┐
│ THE 6 FALSY VALUES IN JAVASCRIPT │
├─────────────┬──────────────────────────────────────────┤
│ false │ The boolean literal false │
│ 0 │ The number zero (and -0, 0n) │
│ "" │ An empty string (length 0) │
│ null │ Intentional empty value │
│ undefined │ Unassigned / missing value │
│ NaN │ Not-a-Number (invalid math result) │
└─────────────┴──────────────────────────────────────────┘
Every other value in JavaScript is TRUTHY!
"0"(a non-empty string containing the character 0) → Truthy!"false"(a non-empty string containing text) → Truthy → Truthy!{}(an empty object) → Truthy!
Interactive Equality & Truthy/Falsy Simulator 🕹️
Use the interactive simulator below to test strict vs. loose comparisons and click values in the Truthy/Falsy matrix:
Compares both the type and the value. If types differ, it returns false instantly without mutating anything.
Silently performs implicit type coercion in the background, frequently causing unexpected bugs in production apps.
Loose == converts the string "5" into the number 5 before comparing (Type Coercion). Strict === checks type first (string !== number) and returns false immediately.
JavaScript Industry Standard: Always write === (triple equals) and !==. This eliminates subtle type coercion bugs and guarantees your equality checks are 100% predictable!
[!TIP] Key Takeaways
- JavaScript has 7 primitive data types:
string,number,boolean,undefined,null,bigint, andsymbol.- Always use
===(strict equality) instead of==to avoid subtle type coercion bugs.- There are only 6 falsy values:
false,0,"",null,undefined, andNaN. Everything else is truthy!