SyntaxStudy
Sign Up
JavaScript Beginner 3 min read

let and const

let and const

let is block-scoped and mutable. const is block-scoped and must be assigned at declaration. Prefer const by default.

Example
const PI = 3.14159;
let count = 0;
count += 1; // OK
// PI = 3; // TypeError
if (true) { let x = 10; } // x not accessible here
for (let i = 0; i < 3; i++) { /* i scoped to loop */ }
Pro Tip

const prevents reassignment, not mutation — const arr = []; arr.push(1) works fine.