Interview Question

Difference between global and local scope?

Global bindings have broad reach; local bindings are limited to their scope.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

Global scope and local scope differ in where their bindings can be accessed. • Global bindings are available through the relevant global environment. • Function and block bindings are local to their lexical region. • Reducing unnecessary global state lowers accidental coupling.

Example

Code
const globalValue = 1;
function read() {
  const localValue = 2;
  return globalValue + localValue;
}

Quick Revision

Global bindings have broad reach; local bindings are limited to their scope.