What Does ‘let’ Do in JavaScript?

In JavaScript, the let keyword is used to declare a local variable with block scope.

Checkout this video:

Introduction

In JavaScript, the ‘let’ keyword is used to declare a block-scoped local variable. The ‘let’ keyword is very similar to the ‘var’ keyword, with one important distinction: variables declared using ‘let’ are not hoisted to the top of the enclosing function or block. This means that you can’t use a ‘let’ variable before it has been declared.

The let keyword

JavaScript let keyword allows you to declare a variable with a block scope. Block scope means that the variable is only available within the curly braces {} where it is declared. The let keyword is similar to the var keyword, with the exception that the var keyword is function scoped and the let keyword is block scoped.

Block scope

In JavaScript, the let keyword is used to create variables with a block scope. Block scope is a curly-brace-delimited region of code in which only certain variables are visible. In other words, variables declared with let are only accessible within the block (or curly braces) they were created in. Consider the following code:

“`javascript
if (true) {
let x = ‘Hello, world!’;
}
console.log(x); // ReferenceError: x is not defined
“`

In the code above, the variable x is created inside a block ({}). This means that x can only be used within that block. If we try to access x outside of its block, we get a ReferenceError. Alternatively, if we declare a variable with const or var, it will be accessible outside of its block:

“`javascript
if (true) {
var y = ‘Hello, world!’; // accessible outside of block
const z = ‘Goodbye, world!’; // also accessible outside of block
}
console.log(y); // ‘Hello, world!’
console.log(z); // ‘Goodbye, world!’;
“`

Temporal Dead Zone

In JavaScript, the let keyword is used to declare variables that are scoped to a block, instead of the function-level scope of the var keyword. This means that variables declared with let are only available within the block they are declared in. For example:

function foo() {
let x = 1;

if (x === 1) {
let y = 2;

console.log(x); // 1
console.log(y); // 2
}

console.log(x); // 1
console.log(y); // ReferenceError
}

foo();
In the foo function above, there are two distinct blocks – the first being everything inside the function foo itself, and the second being everything inside the if statement’s curly braces ({}). The variable x is declared using the let keyword within foo’s block, so it is only available inside of foo. The same goes for y, except it is declared inside the if statement’s block, so it is only available inside that block. Trying to access y outside of the if statement will result in a ReferenceError.

Hoisting

In JavaScript, the let keyword is used to declare a local variable with block scope. Block scope means that the variables declared with let are only accessible within the block they are defined, and not outside of it.

Variables declared with var are accessible anywhere within their containing function, but not outside of it. This is called function scope.

Function scope vs block scope is an important concept to understand, because it can affect the way your code runs.

In JavaScript, variables declared with var are hoisted to the top of their containing function. This means that they are available to be used anywhere within that function, regardless of where they are declared.

block of code.

Conclusion

To sum up, the main difference between “var” and “let” is that “var” is global and “let” is local. “Var” can be accessed anywhere in the program, while “let” can only be accessed within the block it was declared in. In addition, variables declared with “let” can be reassigned, while those declared with “var” cannot.

Scroll to Top