Difference Between const, let and var in JavaScript

// Quick Answer
  • const cannot be reassigned after declaration.
  • let allows reassignment and is block-scoped.
  • var is function-scoped and has older JavaScript behavior.
  • Modern JavaScript mainly uses const and let.

What is the difference between const, let and var?

In JavaScript, const, let, and var are used to create variables. The main differences are scope, reassignment, and hoisting behavior. Modern JavaScript mostly uses const and let because they are safer and easier to manage.

💡 In simple terms

Use const for fixed values, let for changing values, and avoid var in modern code.

How do const, let and var work?

Variables created with var are function-scoped and can be redeclared. Variables created with let and const are block-scoped, meaning they only exist inside the block where they are created. Also, const variables cannot be reassigned after declaration.

example.js
// var example
var city = "Delhi";
city = "Mumbai";

// let example
let age = 20;
age = 21;

// const example
const country = "India";

// Error: cannot reassign const
country = "Canada";

In the example above, var and let can change their values. But const cannot be reassigned after it is created.

Why are const and let important?

const and let make JavaScript code more predictable and reduce bugs. They help developers write cleaner and safer programs compared to older var declarations.

  • Better variable scope management.
  • Prevents accidental redeclaration.
  • Makes code easier to understand and maintain.

Frequently asked questions

Should I always use const?

Yes, use const by default. Only use let when the variable value needs to change later.

Why is var avoided in modern JavaScript?

var can cause unexpected bugs because of hoisting and function scope. Modern JavaScript developers usually prefer let and const.

📌 Good to know

Even if an object is declared with const, its internal properties can still change. Only reassignment is blocked.

Summary

const, let, and var are all used to create variables in JavaScript. Modern developers mainly use const and let because they provide safer and cleaner behavior.

Ready to learn more? Check out our full JavaScript course — it's free and beginner-friendly.