Course Lessons

JAVASCRIPT FOUNDATIONS + AI MINDSET

Back to Course

Variables: var, let, and const

JAVASCRIPT... Lesson 2 of 41 4 min

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.

var greeter = "hey hi" ; var greeter = "say Hello instead" ; // ⚠️ No error — var lets you re-declare freely console . log (greeter); // "say Hello instead"

⚠️ 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.

let greeting = "say Hi" ; let times = 4 ; if (times > 3 ) { let hello = "say Hello instead" ; } console . log (hello); // ❌ hello is not defined

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.

const greeting = "say Hi" ; greeting = "say Hello instead" ; // ❌ error: Assignment to constant variable // ✅ But object properties CAN be updated

✅ 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.

LIVE AI LEARNING SESSION

Ask your AI assistant this question ↓

💬 YOU ASK THE AI:

"What is the difference between let and const in JavaScript? Give me a real-world example of when to use each."

🤖 AI RESPONDS:

Great question. The core difference comes down to one thing: can the value change?

let is for values that will change over time — like a counter, a user's score in a game, or a variable that gets updated inside a loop.

const is for values that should never change — like an API key, a fixed tax rate, or a configuration setting you want to protect from accidental changes.

Real-world example:

// Use const — this rate never changes const TAX_RATE = 0.075 ; // Use let — cart total updates as items are added let cartTotal = 0 ; cartTotal += 49.99 ; // updates fine ✅

📝 MY NOTE — IN MY OWN WORDS

The AI made this click for me immediately. I now think of let as a whiteboard — something I write on and erase regularly — and const as something engraved in stone. In a real shopping cart app, the tax rate is engraved — it never moves. But the cart total is a whiteboard that keeps getting updated every time a customer adds something. Both are block scoped so they stay inside their curly braces and do not leak out and cause chaos elsewhere in the code the way var does. Going forward I will default to const for everything and only switch to let when I know the value needs to change.

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