Skip to content

Variables and Types

Scope: let vs const, the types you'll use, and the === rule that saves beginners weekly. All examples were run with Node v24.

let and const — and never var

let count = 0;        // can be reassigned
count = 1;

const name = "Ada";   // cannot be reassigned
// name = "Bob";      // TypeError: Assignment to constant variable.
  • const is the default. Use it unless you know the value changes.
  • let is for values that genuinely change (counters, accumulators).
  • var is the 1995 original with surprising scoping rules. Pretend it doesn't exist.

Note: const means the binding can't change, not the value — a const array can still be pushed to. It prevents reassignment, not mutation.

The types you'll actually use

Type Example Notes
number 42, 3.14 All numbers are 64-bit floats — no int/float split
string "hello", `hi ${name}` Backticks allow ${} interpolation
boolean true, false
null let x = null "Intentionally empty"
undefined let y; "Never assigned" — different from null
object { name: "Ada", age: 36 } Key-value bundles
array [1, 2, 3] Ordered lists (really objects underneath)
bigint 9007199254740993n Huge integers; the trailing n matters

typeof tells you the type, with two famous lies: typeof null is "object" (a 30-year-old bug, kept for compatibility) and typeof [] is "object" too. Use Array.isArray(x) for arrays.

The === rule

JavaScript has two equalities. Always use === (strict):

0 == "0"    // true  — spooky type coercion
0 === "0"   // false — different types, not equal

== converts types before comparing, producing surprises even veterans can't predict. === compares type and value with no conversion. The rule is absolute: never write == (linters enforce this for a living).

Truthy and falsy

In an if, these count as false: false, 0, "" (empty string), null, undefined, NaN. Everything else counts as true — including [] and {} (yes, really).

if (name) {
  console.log("got a name");
}

This pattern ("is it there and non-empty?") is idiomatic — but remember 0 is falsy, so don't use it to check numbers that could legitimately be zero.

Template literals

const name = "Ada";
const age = 36;
console.log(`Next year ${name} will be ${age + 1}.`);
// Next year Ada will be 37.

Backtick strings with ${} are how you build strings in modern JS. Prefer them over + concatenation.

Destructuring

const user = { name: "Ada", age: 36 };
const { name, age } = user;   // pulls out fields

const [first, second] = [10, 20];  // works on arrays too

You'll see this constantly in real code — it unpacks objects and arrays in one line.

Check

  • [ ] You default to const and can say why var is avoided
  • [ ] You use === always and can explain the == trap
  • [ ] You can list the falsy values from memory

Next: Control Flow.