0Pricing
JavaScript Academy · Lesson

let, const, and avoiding var

let and const basics, why to avoid var, block scope, and a gentle intro to the temporal dead zone (TDZ).

let, const, and avoiding var is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Goal: Use let and const confidently, avoid legacy var, and understand block scope and TDZ.

  • let vs const
  • Block scope
  • TDZ safety
let, const, and avoiding var — illustration 1

let vs const basics

Rule: Use const unless you plan to reassign; otherwise choose let.

// Prefer const by default; use let when you need to reassign
const appName = "CoddyKit";   
// cannot reassign
let score = 0;                
// can reassign

score = score + 1;
console.log("App:", appName);
console.log("Score:", score);
let, const, and avoiding var — illustration 2

Why avoid var

Avoid var because:

  • It is function-scoped, not block-scoped.
  • It is hoisted and can read as undefined before declaration.
  • It makes refactors harder and less predictable.
let, const, and avoiding var — illustration 3

Block scope demo

Block scope helps keep variables local and your code tidy.

// Block scope: variables exist only inside the braces
let lives = 3;

if (true) {
  const bonus = 10;      
 // only inside this block
  let level = 2;          
// only inside this block
  console.log("Inside:", bonus, level);
}

console.log("Lives:", lives);
// The following would be ReferenceError if uncommented:
// console.log(bonus, level);
let, const, and avoiding var — illustration 4

TDZ (safe demo)

TDZ: A let/const variable cannot be read before its declaration line; this prevents many bugs.

// TDZ: accessing let/const before declaration throws ReferenceError
try {
  // Uncommenting next line would throw:
  // console.log(theme);   
  // ReferenceError (TDZ)
  let theme = "dark";
  console.log("Theme after declare:", theme);
} catch (e) {
  console.log("Caught:", e.message);
}
let, const, and avoiding var — illustration 5

Naming & tips

Naming:

  • Use clear nouns for data (userName, score).
  • Use verbs for functions (computeTotal).
  • Keep scope tight; declare close to usage.
let, const, and avoiding var — illustration 6

let vs const quiz

Quick check: Block scope and reassignment.

let, const, and avoiding var — illustration 7

Recap

Recap: Prefer const, use let when you must reassign, avoid var. Block scope keeps variables local, and TDZ guards against early access bugs.

let, const, and avoiding var — illustration 8

Frequently asked questions

Is the “let, const, and avoiding var” lesson free?

Yes — the full text of “let, const, and avoiding var” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “let, const, and avoiding var”?

let and const basics, why to avoid var, block scope, and a gentle intro to the temporal dead zone (TDZ). You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “let, const, and avoiding var” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. let, const, and avoiding var
  2. Block/Function/Module Scope and the TDZ
  3. Naming, Readability, and Common Pitfalls
← Back to JavaScript Academy