JavaScript | Let
let is a keyword used to declare variables in javascript that are block scoped. There are three ways you can declare variables in javascript let, const, var(old). ES6 or ES2015 defines new keywords to declare varaibles in javascript.
Syntax:
let x=1, y=3, z=4;
Block Scope:
A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.
Global Scope:
let num=10; console.log(num); function fun(){ console.log(num); } fun(); // calling the function |
Output:

Function Scope:
function fun(){ let num=10; console.log(num); } fun(); // caling the function console.log(num); |
Output:

Block Scope:
{ let x=23; console.log(x); } console.log(x); |
Output:

Redeclaring Variables in different blocks:
let x=77; { let x=23; console.log(x); } console.log(x); |
Output:

Redeclaring Variables in same blocks:
let x=77; { let x=23; // legal console.log(x); } let x=67;// illegal console.log(x); |
Output:

Does not support Hoisting:
x=12; console.log(x); let x; |
Output:

Recommended Posts:
- How to run JavaScript from PHP?
- Map in JavaScript
- Map.get( ) In JavaScript
- this in JavaScript
- Map.has( ) In JavaScript
- JavaScript Quiz | Set-3
- JavaScript | Date
- DHTML JavaScript
- JavaScript Quiz | Set-1
- JavaScript | match()
- JavaScript Quiz | Set-2
- Math.min( ) In JavaScript
- JavaScript | Date.now()
- JavaScript | DataView()
- Math.max() In JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



