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 to let only when reassignment is genuinely needed.
  • const freezes the binding, not the value — object contents still mutate.
  • Declare variables in the smallest scope that works; avoid globals.

The value types

TypeExampleNotes
number42, 3.14, NaNOne numeric type — integer and float alike
string'hi', `tick ${n}`Immutable; template literals interpolate
booleantrue, false
undefinedlet x;Declared but no value
nullnullDeliberate absence
bigint9007199254740993nIntegers beyond safe range
symbolSymbol('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 trap

Loose == 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.
ValueTruthy?
false, 0, -0, 0nfalsy
'' (empty string)falsy
null, undefined, NaNfalsy
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.

Functions and scope Objects and destructuring

Last refreshed 2026-09-17.