This code snippet was used in var v/s let tutorial of SFDC Stop
// * 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