What is scope in JavaScript?

Scope is the area of the code where a variable is visible.

  • Global scope: declared outside all functions, visible everywhere.
  • Function scope: declared inside a function, visible only there.
  • Block scope: declared with let or const inside { }, visible only in that block.
let g = "global";
function f() {
  let l = "local";
  console.log(g, l);
}
f();
console.log(typeof l);

Output:

Tutorials Class - Output Window

global local
undefined

See JavaScript Scope.


Learn more about the similar topics:
Tutorials
JavaScript Variables
JavaScript Scope
JavaScript Data Types
JavaScript Type Conversion
JavaScript Operators
JavaScript Numbers and Math
Exercises & Assignments
Write a JavaScript program to add two numbers
Write a JavaScript program to swap two variables
Interview Questions & Answers
What are the data types in JavaScript?
What is the difference between == and === in JavaScript?
What is NaN in JavaScript?
What is the difference between null and undefined?
What is scope in JavaScript?
What is the difference between var, let and const?
What is hoisting in JavaScript?