← homeProgramming (Програмування)

What is the difference between var and let in Javascript?

What is the difference between var and let in JavaScript? Why is let preferred?

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In JavaScript, var and let are keywords used for declaring variables, but they have several differences in behavior.

Scope

var has function scope. This means that variables declared using var are defined within the function and are not accessible outside of it.
function example() {
  if (true) {
    var x = 10;
    console.log(x); // accessible
  }
  console.log(x); // accessible
}
let has block scope, meaning it is defined within a block of code (such as in conditional statements or loops) and is not accessible outside of that block.
function example() {
  if (true) {
    let y = 20;
    console.log(y); // accessible
  }
  console.log(y); // not accessible
}

Hoisting

Variables declared with var are hoisted to the top of the function or global object, meaning they can be used before they are actually declared.
console.log(a); // undefined
var a = 5;
console.log(a); // 5
Variables declared with let are also hoisted, but they cannot be used before their declaration.
console.log(b); // ReferenceError: b is not defined
let b = 10;
console.log(b); // 10

Why prefer let?

Block scope:
Avoids unpredictable situations where variables declared with var can be accessed outside of blocks.
No hoisting issues:
Variables declared with let are also hoisted, but using them before declaration results in a ReferenceError. This makes the code easier to understand, as variables do not have an undefined value until they are assigned a value.
Reduced name conflicts:
Using let can help avoid situations where name conflicts arise in the code due to variable name reuse. Since let has block scope, it allows the same name to be used for variables in different blocks of code without conflicts.
Clearer runtime errors:
Using let can lead to more understandable runtime errors in the code. For example, trying to use a variable before its declaration will result in a ReferenceError, making it easier to identify mistakes.
Code cleanliness:
Modern linters prefer let as a more reliable and predictable option for declaring variables. For example, there is a linter for JavaScript called "eslint" that has a rule named "no-var". This rule is designed to detect the use of the var keyword in the code and recommend replacing it with more modern keywords like let or const.

🔥 More posts

All posts
Programming (Програмування)Jul 24, '23 11:02

What is apt-get in Ubuntu?

What is apt-get in Ubuntu? What is apt-get used for?

Computers and technologies (Комп'ютери та технології)Jul 24, '23 12:01

What is "Vulkan Shader Processing" in Steam?

What is "Vulkan Shader Pre-Caching" in Steam? Why is Vulkan shader pre-caching needed?

Programming (Програмування)Dec 7, '23 07:42

What is encapsulation in OOP?

What is encapsulation in OOP? Why is encapsulation (isolation) needed in code?