⚡ Lesson 03 of 30
Variables & Data Types
Explore var, let, const, and JavaScript's seven primitive data types with hands-on examples.
Declaring Variables
JavaScript has three keywords for declaring variables. Prefer const by default; use let when the value will change; avoid var in modern code.
const pi = 3.14159; // cannot be reassigned
let score = 0; // can be reassigned
score = 42;
// var oldStyle = "avoid"; // function-scoped, hoisted
Primitive Types
JavaScript has seven primitive types:
let name = "Alice"; // string
let age = 30; // number
let price = 9.99; // number (no int/float split)
let active = true; // boolean
let empty = null; // null
let undef; // undefined
let id = Symbol("id"); // symbol
BigInt
For integers larger than 2⁵³−1, use the BigInt type by appending n:
const big = 9007199254740993n;
console.log(typeof big); // "bigint"
typeof Operator
Use typeof to inspect a variable's type at runtime:
console.log(typeof "hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (historical quirk)
Type Coercion
JavaScript automatically converts types in some situations — this is called implicit coercion. Understanding it prevents subtle bugs:
console.log("5" + 3); // "53" (number coerced to string)
console.log("5" - 3); // 2 (string coerced to number)
console.log(Boolean(0)); // false
console.log(Boolean(""));// false