Variables

Boxes that hold stuff

What is it?

Variables are like labeled boxes where we store information so we can use it later.

Real-life

Imagine a box with a sticker that says 'age'. Inside is the number 10. The sticker is the variable name; the number is the value.

Key functions & keywords

  • let

    Declare a variable whose value can change later.

    let score = 5;
    score = score + 1;
    console.log(score); // 6
  • const

    Declare a value that must NOT be reassigned.

    const PI = 3.14;
    console.log(PI);
    // PI = 3.2; // TypeError!
  • var

    Old-style variable (function-scoped). Prefer let/const.

    var name = "Riya";
    var name = "Aman"; // var allows redeclare
    console.log(name);
  • typeof

    Returns the type of a value as a string, e.g. typeof 5 → 'number'.

    console.log(typeof 5);     // "number"
    console.log(typeof "hi");  // "string"
    console.log(typeof true);  // "boolean"
  • console.log()

    Prints values to the console for debugging.

    let a = 10, b = 20;
    console.log("sum =", a + b);
    console.log({ a, b });