Skip to content

Day 2: Variables (let & const vs var)

Learn how to store data in memory using let and const, understand why var is obsolete, and master naming conventions.

  • const (Default choice): For values that will not be reassigned. Prevents accidental bugs.
  • let: For variables whose values will change over time (e.g., counters, accumulated totals).
  • var (Legacy): Function-scoped, prone to hoisting errors. Never use var in modern code!
const siteName = 'Fran Academy Docs';
let userCount = 42;
userCount = 43; // OK: reassigning let
// siteName = 'New Name'; // TypeError: Assignment to constant variable
  • Use camelCase: totalPrice, isUserLoggedIn, shippingAddress.
  • Names cannot start with digits and cannot use reserved keywords like class, function, return.
  1. Declare a const for your birth year and a let for your current age.
  2. Increment your age variable by 1 using age = age + 1 and log the result.
  3. Intentionally try reassigning a const and inspect the error in the console.