Every value in JavaScript has a type. There are seven primitive types, but you’ll use five of them daily: strings, numbers, booleans, null, and undefined.
Copy
// String - text wrapped in quotesconst name = "Sarah Doe";const email = 'sarah@example.com';const greeting = `Hello, ${name}!`; // Template literal (more on this next lesson)// Number - integers and decimals are the same typeconst age = 25;const price = 99.99;const temperature = -5;// Boolean - true or falseconst isLoggedIn = true;const hasPermission = false;// Null - intentionally empty ("I know there's nothing here")const selectedItem = null;// Undefined - not yet assigned ("This hasn't been set")let userInput;console.log(userInput); // undefined
That’s it. No int vs float, no str vs bytes. JavaScript keeps it simpler than Python in this regard.
JavaScript
Python
Copy
// One number type for everythingconst count = 42;const price = 19.99;const negative = -10;typeof count; // "number"typeof price; // "number"
Copy
# Separate int and float typescount = 42price = 19.99negative = -10type(count) # <class 'int'>type(price) # <class 'float'>
This is the one part that trips up Python developers. Python has None. JavaScript has two “nothing” values:
Copy
// null = you explicitly set it to "nothing"let selectedUser = null; // "No user selected yet"// undefined = JavaScript set it because nothing was assignedlet searchQuery;console.log(searchQuery); // undefined - "This variable exists but has no value"
Use null when you want to intentionally say “empty” or “no value.” Let undefined be JavaScript’s default for “not yet assigned.” In practice, you’ll mostly use null.
Use typeof to check what type a value is. This is especially useful when debugging or validating API responses.
Copy
typeof "Sarah"; // "string"typeof 42; // "number"typeof true; // "boolean"typeof undefined; // "undefined"typeof null; // "object" ← This is a bug. Seriously.typeof { name: "Sarah" }; // "object"typeof [1, 2, 3]; // "object" ← Arrays are objects too
typeof null returns "object" instead of "null". This is a well-known bug from 1995 that was never fixed because too much code depends on it. To check for null, use value === null instead.
The + operator is particularly tricky — if either side is a string, it concatenates instead of adding. The other math operators (-, *, /) convert strings to numbers.
Don’t rely on type coercion. Always convert types explicitly when you need to. Implicit coercion causes bugs that are hard to track down.
Values that convert to false are called “falsy”: 0, "", null, undefined, NaN, and false. Everything else is “truthy.” You’ll use this concept constantly in React for conditional rendering.
This is the most important syntax rule in this entire lesson.
Copy
// == (loose equality) - converts types before comparingconsole.log(5 == "5"); // true (string "5" converted to number 5)console.log(0 == false); // true (false converted to 0)console.log("" == false); // true (both converted to 0)console.log(null == undefined); // true// === (strict equality) - no type conversion, must match exactlyconsole.log(5 === "5"); // false (different types)console.log(0 === false); // false (different types)console.log("" === false); // false (different types)console.log(null === undefined); // false (different types)
JavaScript
Python
Copy
// Always use === in JavaScriptconst userAge = "25";const minAge = 25;if (userAge === minAge) { console.log("Match"); // Won't run - different types}if (Number(userAge) === minAge) { console.log("Match"); // Runs - both are numbers now}
Copy
# Python's == already works like JavaScript's ===user_age = "25"min_age = 25if user_age == min_age: print("Match") # Won't run - Python doesn't coerce
Always use === and !==. Pretend == and != don’t exist. This is the universal recommendation from every JavaScript style guide, linter, and senior developer.
// ❌ Wrong: Loose equality causes unexpected matchesconst status = 0;if (status == false) { console.log("No status"); // Runs! 0 is coerced to false}// ✅ Correct: Strict equalityconst status = 0;if (status === false) { console.log("No status"); // Doesn't run - 0 is not false}if (status === 0) { console.log("Status is zero"); // Runs - exact match}
== converts types before comparing, which leads to surprising results like 0 == false being true. Use === to compare both value and type.
Confusing null and undefined
Copy
// ❌ Wrong: Treating them as interchangeablelet data;if (data === null) { console.log("No data"); // Doesn't run! data is undefined, not null}// ✅ Correct: Check for both, or use the right onelet data;if (data == null) { console.log("No data"); // Runs - == treats null and undefined as equal}// Or be explicit:if (data === null || data === undefined) { console.log("No data"); // Runs}
undefined means “not yet assigned.” null means “intentionally empty.” They’re not the same thing, but == treats them as equal. This is the one exception where == is sometimes preferred — checking for null == undefined catches both.
Assuming typeof null returns 'null'
Copy
// ❌ Wrong: Checking null with typeofconst result = null;if (typeof result === "null") { console.log("Result is null"); // Never runs!}// ✅ Correct: Direct comparisonconst result = null;if (result === null) { console.log("Result is null"); // Runs}
typeof null returns "object" due to a decades-old bug in JavaScript. Always check for null with === null.
You know the building blocks — strings, numbers, booleans, and how to compare them safely. Next, let’s look at a cleaner way to work with strings using template literals.