Scopes in JavaScript

@mbtamuli

Scope: where to look for things

x = 42;
console.log(y);

The current context of execution. The context in which values and expressions are "visible" or can be referenced or accessed.

Scopes

Scope
var Function
let Block
const Block
function exampleFunction() {
  // x can only be used in exampleFunction
  var x = "declared inside function";  
  console.log("Inside function");
  console.log(x);
}

console.log(x);  // Causes error
let person = "Mriyam"

function otherFunction() {
  person = "Tony";
  {
    let innerPerson = "Mary";
    console.log(innerPerson);
  }
}

otherFunction();

console.log(person); 		// ??
console.log(innerPerson);	// ??
// Tony
Made with Slides.com