← Back to the Build Your Homepage series
⚑
EPISODE 01
let Β· const Β· primitive vs object Β· typeof

Variables & Types

Declare variables properly with let and const, understand JavaScript's primitive and object types, and use typeof to inspect values.

JavaScriptletconsttypestypeof
Duration
⏱ About 1.5 hours
Level
πŸ“Š Beginner
Prerequisite
🎯 Lessons 1–4
OUTCOME
Pick the right declaration for every value and predict its type

What you'll learn

  • 1Choose between let and const (never var)
  • 2Distinguish primitives from objects
  • 3Use typeof to inspect a value's type
  • 4Convert between number, string, and boolean safely

1. let, const, var

javascript
let count = 0;          // mutable
const NAME = "Alice";   // immutable binding
var legacy = true;      // legacy, do not use

count = 1;              // OK
// NAME = "Bob";         // TypeError
  • const by default β€” most variables never need to change
  • let when you must reassign (loop counters, conditional values)
  • var has function scope and hoisting quirks β€” avoid entirely

2. Primitive Types

TypeExampletypeof
number42, 3.14, NaN, Infinity"number"
string"hello", `template`"string"
booleantrue, false"boolean"
undefinedundefined"undefined"
nullnull"object" (a famous bug)
bigint9007199254740993n"bigint"
symbolSymbol("id")"symbol"

3. Objects

javascript
const user = { name: "Alice", age: 30 };
const nums = [1, 2, 3];
const now  = new Date();
typeof user;  // "object"
typeof nums;  // "object" (arrays are objects)
Array.isArray(nums);  // true
πŸ’‘

Use Array.isArray() to detect arrays β€” typeof returns "object" for them.

4. Type Conversion

javascript
Number("42");       // 42
String(42);         // "42"
Boolean(0);         // false
Boolean("");        // false
Boolean("hello");   // true

// Coercion gotchas
"5" + 3;            // "53"  (string concatenation)
"5" - 3;            // 2     (numeric subtraction)
"5" == 5;           // true  (loose equality)
"5" === 5;          // false (strict β€” use this!)
Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub β†—