Created
August 30, 2020 12:21
-
-
Save rahulmalhotra/045e790dea52f23debb3be28bcff7ea1 to your computer and use it in GitHub Desktop.
This code snippet was used in var v/s let tutorial of SFDC Stop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// * Difference between let and var | |
// * let is the new var | |
// * Code:- | |
var person = 'Rahul'; | |
function trailblazer() { | |
var person = 'Trailblazer Rahul'; | |
console.log(person); | |
} | |
console.log(person); | |
trailblazer(); | |
console.log(person); | |
/* | |
Output with var:- | |
Rahul | |
Trailblazer Rahul | |
Rahul | |
*/ | |
/* | |
Output when var is replaced with let:- | |
Rahul | |
Trailblazer Rahul | |
Rahul | |
*/ | |
// * Learnings:- | |
// * var keyword supports function scope | |
// * let keyword also supports function scope | |
// * Code:- | |
var person = 'Rahul'; | |
console.log(person); | |
if(1===1) { | |
var person = 'Trailblazer Rahul'; | |
console.log(person); | |
} | |
console.log(person); | |
/* | |
Output with var:- | |
Rahul | |
Trailblazer Rahul | |
Trailblazer Rahul | |
*/ | |
/* | |
Output when var is replaced with let:- | |
Rahul | |
Trailblazer Rahul | |
Rahul | |
*/ | |
// * Learnings:- | |
// * var keyword doesn't support block scope | |
// * let keyword supports block scope |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment