Variables & types
let, const and var, the primitive types, typeof, and === vs ==.
Programs work with data: names, prices, scores, lists of lessons. A variable is a named box that holds a value so you can use it later. Every value has a type that decides what you can do with it.
Declaring variables: const, let and var#
Rule of thumb: use const by default; switch to let only when you truly need to reassign. Code full of const is easier to read because you know those values never change.
Why not var?
var is the original (pre-2015) keyword. It still works, but it has two surprising behaviours:
varis function-scoped, not block-scoped, so it leaks out ofifblocks and loops.vardeclarations are hoisted and initialised toundefined, so using one before its line givesundefinedinstead of an error.
let and const are block-scoped — they live only inside the nearest { }. You'll dig into this in the Scope, hoisting & closures lesson. In new code, never use var.
Naming rules
- Names may contain letters, digits,
_and$, but can't start with a digit. - They're case-sensitive:
scoreandScoreare different. - Reserved words (
let,class,return…) can't be used. - Convention:
camelCasefor variables and functions (totalPrice),PascalCasefor classes (ShoppingCart), andUPPER_SNAKE_CASEfor true constants (MAX_RETRIES).
const means "no reassignment", not "frozen"#
The primitive types#
JavaScript has seven primitive types — simple, immutable values:
Everything else — arrays, functions, dates, plain {} objects — is an object. JavaScript is dynamically typed: a variable can hold any type, and the type belongs to the value, not the variable.
Checking types with typeof#
Because of these quirks, use Array.isArray(x) for arrays and x === null for null.
undefined vs null#
A good habit: let JavaScript produce undefined, and use null when you want to express "empty".
Numbers: a few gotchas#
All numbers are 64-bit floating point, which is why 0.1 + 0.2 isn't exactly 0.3. For money, store whole units (paise, cents) as integers and format at the end. Integers are only exact up to Number.MAX_SAFE_INTEGER (2⁵³ − 1); beyond that, use bigint.
Template literals (a first look)#
Backticks let you embed expressions with ${} and span multiple lines:
There's a whole lesson on strings coming up.
=== vs ==#
== (loose equality) converts types before comparing, which produces surprising results. === (strict equality) compares type and value:
Always use === and !==. The next lesson explains exactly what == does and the one place some developers still use it.
Truthy and falsy#
In conditions, these eight values count as false (falsy): false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy — including "0", "false", [] and {}.
Common mistakes#
- Using
letfor everything "just in case" — preferconst. - Comparing with
==and being surprised. - Checking
typeof x === "array"— that never works; useArray.isArray(x). - Doing money math with decimals like
19.99 * 3.
What's next#
You can store values and know their types. Next: operators — doing maths, comparing and combining values — and how JavaScript coerces types.
Check your understanding
Quick quiz
1.Which declaration should you use by default for values that won't be reassigned?
2.What is
typeof null?3.Given
const list = [1, 2];, what doeslist.push(3);do?
Finished reading?
Mark this lesson complete to track your progress.