JavaScript basics
Variables, the seven primitives, type coercion, and the == vs === decision that causes most beginner bugs.
Declaring variables
const rate = 0.2; // cannot be reassigned
let count = 0; // can be reassigned
count += 1;
// var is function-scoped and hoisted - avoid it in new code
var legacy = true;- Default to
const; switch toletonly when reassignment is genuinely needed. constfreezes the binding, not the value — object contents still mutate.- Declare variables in the smallest scope that works; avoid globals.
The value types
| Type | Example | Notes |
|---|---|---|
| number | 42, 3.14, NaN | One numeric type — integer and float alike |
| string | 'hi', `tick ${n}` | Immutable; template literals interpolate |
| boolean | true, false | |
| undefined | let x; | Declared but no value |
| null | null | Deliberate absence |
| bigint | 9007199254740993n | Integers beyond safe range |
| symbol | Symbol('id') | Unique keys |
Everything else — arrays, functions, dates, regular expressions — is an object. This is why typeof null returning 'object' is a famous historical bug rather than a rule.
💡
NaN is a number type that means 'not a number', and it is not equal to itself. Use Number.isNaN(x), never x === NaN.Coercion and equality
'5' == 5 // true - coerces types first
'5' === 5 // false - different types
null == undefined // true
null === undefined // false
0 == false // true
'' == false // true
'0' == false // true <- the famous trapLoose == applies conversion rules few people can recite; strict === compares type and value.
⚠️
Always use
===. The only common exception is x == null, which conveniently matches both null and undefined.| Value | Truthy? |
|---|---|
false, 0, -0, 0n | falsy |
'' (empty string) | falsy |
null, undefined, NaN | falsy |
everything else incl. [] and '0' | truthy |
Running your code
console.log('value:', 42);
console.table([{ id: 1, ok: true }]);
// in the browser
<script type='module' src='/app.js'></script>FAQ
Should I still use semicolons?
Either style is fine — pick one and enforce it with a formatter. Automatic semicolon insertion is reliable but has edge cases with lines starting with
(, [, or a backtick.How do I check for an object vs primitive?
typeof identifies primitives (except the null quirk). For array checks use Array.isArray(x), never typeof.Related
Functions and scope Objects and destructuring
Last refreshed 2026-09-17.