To utilize the let keyword to initialize the variable for further, use the following syntax:
- “let” is a keyword used to define a variable.
- “variable_name” indicates the name of the variable that is declared.
- “value” defines the variable value.
To use the let keyword, there multiple methods can be utilized. Some of them are listed below:
- Method 1: Declaring Variable in Global Scope
- Method 2: Declaring Variable in Function Scope
- Method 3: Redeclaring Variables in different blocks
Method 1: Declaring Variable in Global Scope
When a variable is initialized outside of the function and can be accessed anywhere in the program is called the global scope of the variable.
First, create a global variable using the “let” keyword:
Invoke the “console.log()” method and pass the argument to display its value on the console:
Define a function where we will access the global variable “number”:
Now, call the defined function “num()”:
Method 2: Declaring Variable in Function Scope
When the variable is declared within a function and can only be accessed in the function is known as the function scope. Users can declare the variable globally as well as locally.
In this stated example, first, define a function with a particular name and declare a variable with the help of the “let” variable:
Method 3: Redeclaring Variables in Different Blocks
You can also declare the variable in different code blocks. To do so, first, declare the variable globally and then declare it in a different block. However, the inner block will execute first and then outer or globally declare a variable:
Then, initialize another variable using the “let” keyword within the block: