JSJavaScript · Lesson 2 of 14
Variables & Types
JavaScript has three ways to declare variables: var, let, and const. One is old and troublesome, one is modern, and one is modern and immutable. Spot the pattern.
Use const for values that don't change. Use let for values that do. Avoid var — it has confusing scoping rules and function-level scope instead of block scope. Pretend it doesn't exist.
JavaScript is weakly typed, which means it will automatically convert types when comparing values. This is the source of many surprises. Use === (triple equals) for comparison — it checks type AND value.
⚠ Warning
NaN (Not a Number) is the result of invalid math operations like Number("cat") or 0/0. Weirdly, typeof NaN is "number". And NaN !== NaN. Use Number.isNaN() to check for it.
NaN (Not a Number) is the result of invalid math operations like Number("cat") or 0/0. Weirdly, typeof NaN is "number". And NaN !== NaN. Use Number.isNaN() to check for it.