TutreNews Learn

Values and types

Types and equality

Goal: Recognise primitive types and use === safely · 9 min

JavaScript's primitive types: number (one type for integers and decimals), string, boolean, undefined (declared but no value), null (intentionally empty), bigint, symbol. Check with typeof.

JavaScript converts types automatically (coercion), which causes surprises: "5" + 1 is "51" (string concatenation), but "5" * 2 is 10.

Always compare with **=== (strict equality)**: it checks value and type. == converts first: 0 == "" is true, 0 === "" is false.

Falsy values: false, 0, "", null, undefined, NaN. Everything else is truthy.

Worked example: Coercion surprises

  1. "3" + 4 → "34" because + with a string concatenates.
  2. "3" * 4 → 12 because * only works on numbers.
  3. Number("3") + 4 → 7: convert explicitly.
console.log("3" + 4, "3" * 4, Number("3") + 4);

Output:

34 12 7