JavaScript let Tutorial

In this section we will learn what JavaScript let keyword is and how to use it.

What is JavaScript let?

The let keyword is used to create variables that are block scope.

Meaning of Block Scope in JavaScript

First of all, you might be wondering what’s a block and what’s a scope?

  • Block: In JavaScript, a block is the area between a pair of opening and closing curly brackets `{}`. Basically, the curly brackets set the boundary of that block.

So if you have a function, the block of that function starts from the opening curly bracket `{` and ends right at the closing curly bracket `}`.

  • Scope: we talk about scope when it comes to an identifier like the name of a function or a variable. Scopes basically refer to the area in which an identifier (a variable, for example) is accessible and could be invoked to get or set its value.

So now that we know the meaning behind a `block` as well as a `scope`, let’s see what does it means to say “block scope”.

Block scope mainly used when talking about variables, especially those that are created via the `let` keyword.

Variables that are created using the `let` keyword are block scope. That means the area in which the variable is accessible is limited to the enclosing a pair of curly brackets. Outside of the enclosing curly brackets, it’s like the variable didn’t exist at all! So trying to access them from outside would cause a problem in your program.

How to Declare Variable in JavaScript via let keyword: Syntax

In order to create a variable by let keyword, we first put the let keyword and on the right side of this keyword comes the variable name that we want to create.

let variab_name;

After the execution of this statement by a computer, the “variable_name” becomes alive and it is accessible only within the body of the block in which it was declared.

Example: creating a variable via JavaScript let keyword

{
   let name = "John";
}
alert(name);

Here there’s an anonymous block scope (which is a pretty acceptable block) and inside this block we have a variable named “name” with the value “John”.

As mentioned before, a block level variable (created via the `let` keyword) is not accessible outside of the enclosing block.

So here, even though the `name` variable was created inside a block with the `let` keyword, we’ve tried to access the variable from outside but then no value appeared on the screen! This is because from an outside perspective, the variable `name` does not exist and so trying to access a variable that doesn’t exist will give you nothing!

(In some situations which you’ll see in the future lessons, it will cause a program to even crash if you try to access an identifier that does not exist in the first place).

Global JavaScript Variables and let keyword

If you create a variable outside the boundary of any block, that variable is considered a global variable no-matter if you use the `let` keyword or the `var` keyword.

This means such a variable is accessible from anywhere in the program.

Example: creating global JavaScript variables via let keyword

let globalVariable = "I'm a global variable";
function callGlobe(){
   alert(globalVariable);
}
callGlobe();

In this example, we have one global variable called `globalVariable`. This is a global variable because it is declared outside of any block.

Now within the body of `callGlobe()` function we can access this variable and invoke its value. (Again, this is only because the variable is declared globally).

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies