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:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- Map.has( ) 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.



