Overview
To declare the variables in JavaScript or TypeScript, we often use var or let. Here, we will discuss the differences between both of the declarations.
var
When we declare variables using var:
- The declaration is processed before the execution.
- Variable will have function scope.
Code
- function CheckingScope() {
- var _scope = ‘Out’;
- console.log(_scope); // output Out
- if (true) {
- var _scope = ‘In’;
- console.log(_scope); // output In
- }
- console.log(_scope); // output In
- }
In the above code, we can find that when the variable is updated inside the if block, the value of variable "_scope" is updated to “In” globally.
let
When we declare variables using let:
- Variable will have block scope.
Code
- function CheckingScope() {
- var _scope = ‘Out’;
- console.log(_scope); // output Out
- if (true) {
- let _scope = ‘In’;
- console.log(_scope); // output In
- }
- console.log(_scope); // output Out
- }
In the above code, we can find that when the variable is updated inside the if block, the value of variable "_scope" is not updated globally.
Reference
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Amit Kumar SinghPosted Feb 16, 2020, 9:34 AM
Thanks for sharing !!!
Laxmidhar SahooPosted Oct 27, 2018, 1:43 AM
Good example
Mohit VermaPosted Mar 27, 2018, 11:45 PM
There is one mistake in your let example code. There variable deceleration will be let not var.
Pawan Kumar TiwariPosted Mar 27, 2018, 11:07 PM
You can check that example both place declare using var keyword