Skip to content

Instantly share code, notes, and snippets.

@rahulmalhotra
Created August 30, 2020 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulmalhotra/045e790dea52f23debb3be28bcff7ea1 to your computer and use it in GitHub Desktop.
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
// * 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