Last active
February 1, 2025 15:53
-
-
Save Chalarangelo/c527a92224c15e46edfb4e0807ef30fc 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 someFunction(){ | |
if (true){ | |
// This variable is defined inside these | |
// curly braces, which means its part of the | |
// scope of this block and cannot be accessed | |
// by other parts of someFunction. | |
let blockVariable = 30; | |
// However, using the 'var' keyword this variable, | |
// while defined inside the same block, is part of | |
// the scope of the someFunction. | |
var functionVariable = 30; | |
// Inside the block, both variables are accessible. | |
console.log("Block scope: "); | |
console.log(blockVariable); | |
console.log(functionVariable); | |
} | |
// Outside the block, only the variable defined using | |
// the keyword 'var' is accessible. | |
console.log("Function scope: "); | |
try { console.log(blockVariable); } | |
catch (e) { console.log("The variable 'blockVariable' is not accessible!"); } | |
try { console.log(functionVariable); } | |
catch (e) { console.log("The variable 'functionVariable' is not accessible!"); } | |
} | |
someFunction(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment