Back to JavaScript Fundamentals

What is the difference between const, var, and let in JavaScript?

There are three ways to declare variables in JavaScript: const, var, and let. But what is the difference between them?
6 min readLogan FordLogan Ford
Difference between const, var and let in JavaScript

What is the difference between const var and let in JavaScript?

Understanding variable declarations is crucial in JavaScript. Let's explore the three ways to declare variables: const, var, and let, and understand their key differences and use cases.


What is const in JavaScript?

const is used to declare constants - variables whose values cannot be reassigned after initialization. This makes your code more predictable and helps prevent accidental changes.


1const name = "John";
2name = "Jane"; // TypeError: Assignment to constant variable
3
4// However, for objects and arrays:
5const user = { name: "John" };
6user.name = "Jane"; // This works - only reassignment is prevented

const variables are block-scoped, meaning they are only accessible within the block they are declared in (like within if statements or loops).


What is var in JavaScript?

var is the original way to declare variables in JavaScript. It has some unique behaviors that can sometimes lead to unexpected results.


1var name = "John";
2name = "Jane"; // This works fine
3
4// Function scope example
5function example() {
6 var x = 1;
7 if(true) {
8 var x = 2; // Same variable!
9 console.log(x); // 2
10 }
11 console.log(x); // 2
12}
13
14// Hoisting example
15console.log(hoisted); // undefined
16var hoisted = "I am hoisted";

var is function-scoped rather than block-scoped, and it gets hoisted to the top of its scope. These behaviors can make code harder to understand and maintain, which is why modern JavaScript tends to avoid var.


What is let in JavaScript?

let provides a more predictable way to declare variables that need to be reassigned. It combines the flexibility of var with better scoping rules.


1let count = 1;
2count = 2; // This works fine
3
4// Block scope example
5function example() {
6 let x = 1;
7 if(true) {
8 let x = 2; // Different variable
9 console.log(x); // 2
10 }
11 console.log(x); // 1
12}
13
14// No hoisting behavior
15console.log(notHoisted); // ReferenceError
16let notHoisted = "I am not hoisted";

When should you use const var or let?

Here are some guidelines for choosing between these declarations:

  1. Use const by default

    • For values that won't be reassigned
    • For object and array references
    • Makes code intentions clearer
  2. Use let when you need to reassign

    • For counters in loops
    • For values that change over time
    • When working with mutable state
  3. Avoid var in modern code

    • Legacy code still uses it
    • Can lead to scope confusion
    • Lacks block scoping benefits

Summary

  • const: For values that won't be reassigned, block-scoped
  • var: Legacy declaration, function-scoped, hoisted
  • let: For reassignable values, block-scoped, no hoisting

Looking for more?

Ready to test your understanding of variable declarations in JavaScript? Try our practice challenges below!


How to Create a Personalized Greeting Message Using JavaScript String Concatenation
Beginner
VariablesStringsConcatenation
JavaScript While Loop Not Incrementing Counter Variable - Infinite Loop Issue
Beginner
JavascriptVariablesIncrement+3
How Does Variable Reassignment Work in JavaScript: Number to String Conversion
Beginner
JavascriptVariablesTypes

Land your dream tech job faster

Join 810+ developers who are accelerating their coding skills with TechBlitz.

Share this article