Global Scope

Scope is a context in which our variables are declared. We think about scope in relation to blocks because variables can be defined either inside or outside of the block. In global scope variables are declared outside any block so that they can be accessible to all the blocks.

Example:

let color = "blue";

function favColor() {
  console.log(`My favourite color is ${color}.`);
}

favColor();

// Output: My favorite color is blue.

Example:

let satellite = "The Moon";
let galaxy = "The Milky Way";
let stars = "North Star";

function callMyNightSky() {
  return "Night Sky: " + satellite + ", " + stars + ", and " + galaxy;
}

console.log(callMyNightSky());

// Output:

Last updated