What is the difference between global, function, and block scope?

Beginner

Answer

  • Global scope: Variables declared outside any function/block
  • Function scope: Variables declared inside a function (var, parameters)
  • Block scope: Variables declared inside {} blocks (let, const)
var globalVar = "global";
function example() {
  var functionScoped = "function";
  if (true) {
    let blockScoped = "block";
    const alsoBlock = "block";
  }
}