Tutorials Modern JavaScript (ES6+) for Beginners

let, const & Block Scope

Learn let, const & Block Scope in our free Modern JavaScript (ES6+) for Beginners series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
let, const & Block Scope
Lesson 2 of 12 · Part 1 — Modern Basics · Modern JavaScript (ES6+) for Beginners
Course: Modern JavaScript (ES6+) for Beginners · Lesson: 2/12 · Read time: ~14 min · Level: Beginner · ES version: ES2015 (ES6) and above

let, const & Block Scope

Before ES6, JavaScript had only var. It had confusing rules — variables could “leak” outside if blocks and loops. ES6 added let and const with block scope: variables live only inside the { } where you declare them.

const — value you will not reassign

const API_URL = 'https://api.toolliyo.com';
const MAX_LOGIN_ATTEMPTS = 5;

// API_URL = 'other';  // ❌ Error — cannot reassign const

Use const by default for variables that should not point to a new value.

let — value that can change

let score = 0;
score = score + 10;  // ✅ OK
score += 5;

Block scope in action

if (user.isLoggedIn) {
  const welcome = 'Hello, ' + user.name;
  let visitCount = 1;
}
// console.log(welcome);  // ❌ Error — not visible outside block
🌍 Real-world example — Shopping cart item count
function updateCart(items) {
  let total = 0;
  for (const item of items) {
    const lineTotal = item.price * item.qty;  // const — per iteration
    total += lineTotal;
  }
  return total;
}

updateCart([
  { name: 'Node.js ebook', price: 499, qty: 1 },
  { name: 'React course', price: 999, qty: 2 }
]); // 2497

const with objects and arrays

const prevents reassignment, not mutation:

const user = { name: 'Alex', role: 'student' };
user.role = 'admin';     // ✅ OK — editing property
// user = {};            // ❌ Error — reassigning variable

const tags = ['javascript', 'es6'];
tags.push('react');      // ✅ OK
// tags = [];            // ❌ Error
⚠️ Common Mistake: Using var in new code. var is function-scoped and hoisted in confusing ways — avoid it in 2026 projects.
💡 Tip: Rule of thumb: const always, let when you need to reassign, never var.
👨‍🏫 Teaching note: Demo the loop bug: for (var i=0; i<3; i++) setTimeout(() => console.log(i), 100) prints 3,3,3. With let it prints 0,1,2.

Continue learning

Previous: Introduction — Why ES6+ Before Node.js & React

Next: Arrow Functions Explained

Course home: All 12 lessons

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain JavaScript in the context of Modern JavaScript (ES6+) for Beginners.
Short answer: JavaScript runs single-threaded with an event loop. Closures capture lexical scope; promises/async handle I/O without blocking the UI thread. How to structure your answer (60–90 seconds) Define JavaScript i…
Mid Detailed
What are common mistakes teams make with Components when using Modern JavaScript (ES6+) for Beginners?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Components in plain language…
Senior Detailed
How would you debug a production issue related to State in a Modern JavaScript (ES6+) for Beginners application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define State in plain language for…
Mid Detailed
Compare two approaches to API integration—when would you choose each?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define API integration in plain lan…
Junior Detailed
Describe a real-world scenario where Performance mattered in a Modern JavaScript (ES6+) for Beginners project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Performance in plain languag…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Modern JavaScript (ES6+) for Beginners
Course syllabus
Part 1 — Modern Basics
Part 2 — Write Less Code
Part 3 — Data & Collections
Part 4 — Async & Modules
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details