Video Lesson

Lesson Notes

JavaScript Variables (let & const) Explained

JavaScript introduced let and const to provide clearer and safer ways to declare variables compared to the legacy var keyword.

Using let

  • Purpose: For values that may change over time.
  • Scope: Block-scoped (only exists inside the nearest {} block).
1let count = 5; // initial value 2count = 10; // update later 3console.log(count); // Outputs: 10

Key Points for let:

  • Prevents accidental global variables.
  • No hoisting surprises—you can’t use it before declaration.

Using const

  • Purpose: For values that should remain constant.
  • Scope: Block-scoped (same as let).
1const pi = 3.14; // fixed value 2// pi = 3.14159; // Error! Cannot reassign a const 3console.log(pi); // Outputs: 3.14

Key Points for const:

  • Guarantees the variable won’t be reassigned.
  • Encourages safer, more predictable code.

Recommendation:

  • Use const by default for any variable that doesn’t need reassignment.
  • Use let when you know the value will change.
  • Together, let and const help you write clearer, more maintainable JavaScript.

AI Assistant

Sign In Required

Sign in and subscribe to use the AI assistant for instant help with your lessons.

Sign In

Variables with "const" and "let" keywords

A quick overview of when and how to use let and const in modern JavaScript. JavaScript introduced let and const to provide clearer and safer ways to declare variables compared to the legacy var keyword.

3m 48s

Course Content

0 of 32 lessons completed
6. Promises, Async and Await
0 of 0 completed
7. The DOM
0 of 0 completed