Skip to main content

Primitives

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.
That’s it. No int vs float, no str vs bytes. JavaScript keeps it simpler than Python in this regard.

null vs undefined

This is the one part that trips up Python developers. Python has None. JavaScript has two “nothing” values:
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.

Type checking

Use typeof to check what type a value is. This is especially useful when debugging or validating API responses.
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.
To check if something is an array, use Array.isArray():

Type coercion

JavaScript tries to be “helpful” by automatically converting types when you mix them. This leads to some genuinely weird behavior.
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.

Converting types explicitly

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.

Equality: == vs ===

This is the most important syntax rule in this entire lesson.
Always use === and !==. Pretend == and != don’t exist. This is the universal recommendation from every JavaScript style guide, linter, and senior developer.

Common mistakes

== converts types before comparing, which leads to surprising results like 0 == false being true. Use === to compare both value and type.
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.
typeof null returns "object" due to a decades-old bug in JavaScript. Always check for null with === null.

What’s next?

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.

Template literals

String interpolation, multi-line strings, and goodbye to string concatenation