Variables: var, let, and const
Var, Let, and Const
What's the Difference?
By Sarah Chima Atuonwu · JavaScript · ES6 · Variables
🤖 AI-POWERED LESSON
This article is paired with a live AI session. As you read, you will see real questions asked to an AI assistant and the answers that came back — showing you exactly how to use AI to learn JavaScript faster.
A lot of shiny new features came out with ES2015 (ES6). One of the most important was the addition of let and const — two new ways to declare variables alongside the original var. If you are still not clear on what makes them different, this article — and your AI assistant — will clear it all up.
We will discuss all three with respect to their scope, use, and hoisting — and at each stage, see how AI can deepen your understanding instantly.
var
THE ORIGINAL · GLOBALLY OR FUNCTION SCOPED
Before ES6, var was the only way to declare variables. Its scope is either global (when declared outside a function) or function-scoped (when declared inside one). It can be both re-declared and updated freely — which turned out to be a significant source of bugs.
⚠️ THE PROBLEM WITH VAR
If you accidentally re-declare a var variable somewhere else in your code, JavaScript will not warn you. The original value silently gets overwritten — and this is a very common source of hard-to-find bugs.
let
THE MODERN CHOICE · BLOCK SCOPED · UPDATABLE
let is now the preferred way to declare variables. It is block scoped — meaning it only exists inside the curly braces {} where it was declared. It can be updated, but it cannot be re-declared in the same scope.
const
CONSTANTS ONLY · BLOCK SCOPED · NEVER RE-DECLARED
const maintains a constant value. Like let, it is block scoped. Unlike both var and let, it cannot be updated or re-declared. It must also be initialized at the moment of declaration.
✅ SPECIAL CASE — CONST WITH OBJECTS
A const object cannot be reassigned entirely — but its individual properties can still be changed. So greeting.message = "new value" is perfectly valid even on a const object.
Quick Comparison
| FEATURE | var | let | const |
|---|---|---|---|
| Scope | Global / Function | Block | Block |
| Can be updated? | ✅ Yes | ✅ Yes | ❌ No |
| Can be re-declared? | ✅ Yes | ❌ No | ❌ No |
| Hoisted? | ✅ as undefined | ⚠️ not initialized | ⚠️ not initialized |
| Must be initialized? | ❌ No | ❌ No | ✅ Yes |
🤖 HOW AI ACCELERATES THIS TOPIC
Struggling to remember which one to use? Try pasting your own code into an AI and asking: "Am I using var, let, or const correctly here? Should any of these change?" The AI will review your specific code, explain the logic, and suggest improvements instantly.
You can also ask: "Why did my let variable give me a ReferenceError?" — and the AI will walk you through hoisting, temporal dead zones, and block scope in plain English, using your exact code as the example.
Article by Webbo3· ES6 Variable Declarations · JavaScript Learning with AI
Complete this lesson
Mark as complete to track your progress