Understanding the Differences Between var, let, and const in JavaScript

Sangwin Gawande
3 min readOct 29, 2024
Understanding the Differences Between var, let, and const in JavaScript
Understanding the Differences Between var, let, and const in JavaScript

In JavaScript, you can use three keywords to declare variables: var, let, and const. Each of these has its own rules and best uses. Let’s break them down in simple terms.

1. What is var?

var is the oldest way to declare a variable in JavaScript. Here are some key points about var:

  • Function Scope: If you declare a variable with var inside a function, it can only be used inside that function. If you declare it outside, it becomes a global variable, which means it can be accessed anywhere in your code.
  • Hoisting: When the JavaScript engine runs your code, it moves (or “hoists”) all var declarations to the top. This means you can use the variable before you declare it in your code.
  • Redeclaration: You can declare the same variable name multiple times in the same scope without any errors.

Example:

function example() {
console.log(a); // Outputs: undefined (due to hoisting)
var a = 5;
console.log(a); // Outputs: 5
}
example();

2. What is let?

let was introduced in ES6 (a version of JavaScript) to provide better variable handling. Here’s what you should know:

  • Block Scope: Variables declared with let are only accessible within the nearest block (like loops or if statements). This makes it easier to manage your code.
  • No Hoisting Issues: While let is also hoisted, you cannot use it before declaring it. This prevents errors related to variable usage before it’s defined.
  • No Redeclaration: You cannot declare the same variable name more than once in the same scope.

Example:

if (true) {
let b = 10;
console.log(b); // Outputs: 10
}
// console.log(b); // Error: b is not defined

3. What is const?

const is similar to let, but with one important difference:

  • Block Scope: Like let, const is also block-scoped.
  • No Reassignment: You cannot change the value of a variable declared with const after it has been assigned. However, if the variable holds an object or array, you can still change its properties or elements.

Example:

const c = 20;
// c = 30; // Error: Assignment to constant variable
const myArray = [1, 2, 3];
myArray.push(4); // This is allowed
console.log(myArray); // Outputs: [1, 2, 3, 4]

When to Use Each One

  • Use var: While var is still available, it is generally not recommended for new code. It can lead to confusion due to its scope rules and hoisting behavior.
  • Use let: Choose let when you expect the variable to change. It’s great for variables that will be reassigned in loops or other blocks of code.
  • Use const: Use const when you want to declare a variable that should not change. This is a good practice for values you don’t want to accidentally modify.

Closure

Understanding the differences between var, let, and const is essential for writing clean and effective JavaScript code. By using let and const, you can write clearer and more manageable code, making it easier for yourself and others to read and maintain.

Cheers!!

Sangwin Gawande

About me : https://sangw.in

--

--

No responses yet