Day 2: Variables (let & const vs var)
Daily Objective
Section titled “Daily Objective”Learn how to store data in memory using let and const, understand why var is obsolete, and master naming conventions.
1. const vs let vs var
Section titled “1. const vs let vs var”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 usevarin modern code!
const siteName = 'Fran Academy Docs';let userCount = 42;
userCount = 43; // OK: reassigning let// siteName = 'New Name'; // TypeError: Assignment to constant variable2. Naming Rules (camelCase)
Section titled “2. Naming Rules (camelCase)”- Use camelCase:
totalPrice,isUserLoggedIn,shippingAddress. - Names cannot start with digits and cannot use reserved keywords like
class,function,return.
Daily Hands-on Practice (70%)
Section titled “Daily Hands-on Practice (70%)”- Declare a
constfor your birth year and aletfor your current age. - Increment your age variable by 1 using
age = age + 1and log the result. - Intentionally try reassigning a
constand inspect the error in the console.