skip to Main Content

Difference Between var, let and const in JavaScript

Difference Between var, let and const in JavaScript

Table of Contents

  1. What is var in JavaScript?
  2. What is let in JavaScript?
  3. What is const in JavaScript?
  4. Key Differences Between var, let and const
  5. When to Use var, let, or const
  6. Conclusion
  7. FAQs

When writing JavaScript code, we use variables to store data. There are three main ways to declare variables:

  • var
  • let
  • const

But what’s the difference between them? In this post, we’ll explain them simply, with examples and best practices, so you’ll know when to use which.


What is var in JavaScript?

The var keyword is the old way to declare variables in JavaScript. It has function scope, not block scope.

Example:

function example() {
  console.log(a); // undefined
  var a = 10;
  console.log(a); // 10
}
example();

Features of var:

  • Scoped to functions, not blocks.
  • Can be redeclared.
  • Can be reassigned.
  • Hoisted (moved to the top), but initialized as undefined.

What is let in JavaScript?

The let keyword was introduced in ES6 (2015). It is block-scoped, meaning it works only within the {} block where it’s declared.

Example:

let name = "John";
name = "Jane"; // ✅ allowed

let name = "Doe"; // ❌ SyntaxError: Identifier 'name' has already been declared

Features of let:

  • Scoped to blocks (if, for, {}).
  • Cannot be redeclared in the same scope.
  • Can be updated.
  • Hoisted, but not initialized — using before declaration gives ReferenceError.

What is const in JavaScript?

const is also introduced in ES6. It is used to declare constant values.

Example:

const PI = 3.14;
PI = 3.14159; // ❌ TypeError: Assignment to constant variable.

But remember: for objects and arrays, you can change the contents, just not reassign them.

Example:

const person = { name: "John" };
person.name = "Jane"; // ✅ allowed

person = {}; // ❌ TypeError

Features of const:

  • Scoped to blocks.
  • Cannot be redeclared or reassigned.
  • Must be initialized at declaration.

Key Differences Between var, let and const

Featurevarletconst
ScopeFunctionBlockBlock
Redeclare✅ Yes❌ No❌ No
Reassign✅ Yes✅ Yes❌ No
Hoisting✅ Yes (undefined)✅ Yes (no init)✅ Yes (no init)
ES6 Support❌ Old✅ Yes✅ Yes

When to Use var, let or const

Use let when the value can change (like a counter, loop variable, etc.).

Use const when the value should not change (like configuration settings, fixed values).

Avoid var in modern JavaScript. It’s outdated and can cause confusing bugs due to its function-scoping.


Conclusion

Understanding the difference between var, let, and const is important for writing clean and modern JavaScript code.
Tip: Stick to let and const for better code and fewer errors.


FAQs

Can I use var and let in the same block?

You can, but it’s not recommended. Use let or const to avoid confusion.

Is const completely read-only?

You can’t reassign the variable, but you can change contents of arrays and objects.

Why is let better than var?

let respects block scope and avoids issues related to redeclaration and hoisting.

What happens if I forget to initialize a const variable?

You’ll get a SyntaxErrorconst must be initialized when declared.


I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top