Created
June 20, 2021 03:26
-
-
Save pszponder/1b20918160fcfd7e4f62343b65d584c5 to your computer and use it in GitHub Desktop.
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
function myFuncVar() { | |
// Declare a variable using var inside myFunc's funciton scope | |
var x = 1; | |
console.log(x); | |
if (true) { | |
// Since var is function scoped and not block scoped, | |
// we are actually overwriting the value of the x variable | |
var x = 2; | |
console.log(x); | |
} | |
// Will print out 2 since we overwrote the variable in the if statement | |
console.log(x); | |
} | |
function myFuncLet() { | |
// Declare a variable using var inside myFunc's funciton scope | |
let x = 1; | |
console.log(x); | |
if (true) { | |
// Creating a new variable x = 2 inside the block scope | |
// (this is not accessible outside of the if statement's curly braces) | |
let x = 2; | |
console.log(x); | |
} | |
// 1 since console.log() cannot access x = 2 variable defined | |
// in the if statement due to block scope | |
console.log(x); | |
} | |
// Call the functions | |
myFuncVar(); | |
myFuncLet(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment