Home / Topic Library / JavaScript / Variables
⚡ JavaScript Topic Library

JavaScript Variables — let, const & var

Three keywords create variables in JavaScript, and two of them exist mostly so you can avoid the third. Learn the modern rules and scope stops being scary.

⚡ Quick Answer

Use const by default, let when the value must change, and never var in new code. const and let are block-scoped (they live inside the nearest { }); var is function-scoped and hoisted, which causes classic bugs.

§let and const — the modern pair

modern.js
const site = "BitWithBite";   // can't be reassigned
let score = 0;                 // can change
score += 10;                   // fine
// site = "other";             // TypeError: Assignment to constant

const user = { name: "Ada" };
user.name = "Alan";            // OK! const locks the BINDING,
                               // not the object's contents

§Block scope

scope.js
if (true) {
  let inside = "visible only here";
  const also = "me too";
}
// console.log(inside);   // ReferenceError — gone outside the block

for (let i = 0; i < 3; i++) { /* i lives only in the loop */ }
// console.log(i);        // ReferenceError

§Why var causes bugs

var_trap.js
console.log(ghost);   // undefined (not an error!) — var hoists
var ghost = "boo";

for (var j = 0; j < 3; j++) {}
console.log(j);        // 3 — var leaks out of the loop!

// same code with let would throw helpful errors instead

§Naming conventions

naming.js
const userName = "ada";        // camelCase — the JS standard
const MAX_LIVES = 3;           // ALL_CAPS for true constants
const _internal = "private-ish";
// const 2fast = "no";          // can't start with a digit
// const class = "no";          // reserved word

§Common Mistakes to Avoid

🚨 Watch out for these
  • Using var in new code — var hoists and leaks out of blocks — modern codebases and linters ban it; use const/let
  • Thinking const makes objects immutable — const prevents reassignment of the name — object properties and array items can still change
  • Redeclaring with letlet x = 1; let x = 2; in the same scope is a SyntaxError

§Frequently Asked Questions

Should I use let or const?

const by default — it documents that the binding never changes. Switch to let only when you actually reassign, like counters and accumulators.

What is hoisting in JavaScript?

Declarations are processed before code runs. var variables are hoisted and initialized to undefined; let/const are hoisted but locked in a 'temporal dead zone' until their line runs, so using them early throws an error.

Why can I change a const object?

const freezes the variable binding, not the value. To make an object's contents immutable use Object.freeze(obj).

Is var ever needed?

No — there is no situation in modern JavaScript where var is required. It survives only in legacy code.

🎓 Want to master JavaScript properly?This reference covers one topic — the full free course takes you from zero to real projects, with quizzes and a certificate.
Start the Free JavaScript Course →

§Related JavaScript Topics

Data Types →