All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is the difference between var and let in Javascript?

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.

This post doesn't have any additions from the author yet.

24 Jul 11:02

What is apt-get in Ubuntu?

meme code
meme code@memecode
24 Jul 11:25

How to install Steam on Ubuntu? Installing Steam via the terminal.

meme code
meme code@memecode
24 Jul 11:47

How to change the language in Steam to Ukrainian?

meme code
meme code@memecode
24 Jul 12:01

What is "Vulkan Shader Processing" in Steam?

meme code
meme code@memecode
24 Jul 12:17

How to make Counter-Strike: GO fullscreen in Ubuntu without launching the game?

meme code
meme code@memecode
20 Nov 19:35

How to recover a deleted file (Trix) that was stored on Amazon S3?

meme code
meme code@memecode
07 Dec 07:42

What is encapsulation in OOP?

meme code
meme code@memecode
07 Dec 08:13

Visibility control in Ruby (public, private, and protected)

meme code
meme code@memecode
07 Dec 08:25

What is OOP (object-oriented programming)?

meme code
meme code@memecode
09 Dec 12:00

What is inheritance in Ruby? Examples of bad and good inheritance.

meme code
meme code@memecode
09 Dec 12:15

What is best practice in programming?

meme code
meme code@memecode
09 Dec 12:21

What is polymorphism? An example of using polymorphism in Ruby.

meme code
meme code@memecode