Difference between var and let in Javascript

kutt27

Amal Satheesan

Posted on April 10, 2024

Difference between var and let in Javascript

Why Two Ways to Declare Variables in JavaScript?

When you're starting with JavaScript, you might wonder why there are two ways to declare variables (var and let).

Different Methods for Variable Declaration

JavaScript offers multiple keywords for variable declaration:

  • var
  • let
  • const (introduced in ES6)

Here are some examples:

var num1 = 5;
let name1 = "Someone";
const pi = 3.14;  // Constant value (cannot be reassigned)
Enter fullscreen mode Exit fullscreen mode

Reasons for let and const (Introduced in ES6)

ES6 introduced const and let to address limitations with var. While var allowed variable declaration, it caused issues due to:

  • Hoisting: Variables declared with var are accessible before their declaration, leading to unexpected behavior.
  • Scope: var is function-scoped, meaning variables can be accessed throughout the entire function regardless of code blocks. This can make code harder to reason about.

const and let fixed these issues by introducing block-level scoping, preventing unexpected behavior and making code more predictable and maintainable.

The Problem with var for Beginners

Without proper understanding, beginners might not be familiar with the differences between let and var. This can lead to confusion and potential bugs in their code.

Understanding Scope

  • var: Function-scoped. A variable declared with var is accessible throughout the entire function it's declared in, regardless of code blocks.
  • let: Block-scoped. A variable declared with let is only accessible within the code block (like an if statement or loop) where it's declared.

Example: Scope Difference

function letSci() {
  let x = 1;
  if (true) {
    var y = 2;
    x = 2; // Modifying `x` declared outside the block with `let` is allowed
    console.log(x); // Output: 2
  }
  console.log(x); // Output: 2 (x is accessible here)
}

letSci();

// y can be accessed here because it's function-scoped with var
console.log(y); // Output: 2 (assuming y was declared before letSci())
Enter fullscreen mode Exit fullscreen mode

Hoisting and Redeclaration

  • var: Hoisted to the top of their function scope. You can access them even before they are declared in the code.
  • let: Not hoisted. You cannot access them before their declaration.

This prevents errors that might occur with var due to accessing an undeclared variable.

Example: Hoisting Difference

console.log("var section\n");

console.log(y); // This will result in an error because `y` is declared with `var` later in the code
var y = 10;
console.log(y); // Output: 10

console.log("let section\n");

console.log(x); // This will result in an error because `x` is declared with `let` and cannot be accessed before its declaration
let x = 5;
console.log(x); // Output: 5
Enter fullscreen mode Exit fullscreen mode

Additional Considerations

While not covered here due to space limitations, exploring const is also recommended. It's used for variables that shouldn't be reassigned after their initial declaration.

By understanding the differences between var, let, and const, you can write cleaner, more maintainable JavaScript code.

💖 💪 🙅 🚩
kutt27
Amal Satheesan

Posted on April 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How JavaScript works
javascript How JavaScript works

November 8, 2024

A Beginner's Guide to JavaScript Closures
javascript A Beginner's Guide to JavaScript Closures

November 5, 2024

Type Coercion in JavaScript Explained
javascript Type Coercion in JavaScript Explained

November 18, 2024

Introduction to Asynchronous JavaScript
javascript Introduction to Asynchronous JavaScript

October 14, 2024

Strings -- Manipulating the Immutable.
javascript Strings -- Manipulating the Immutable.

November 15, 2024