Skip to main content
D
JavaScript
JavaScript Scope

JavaScript Scope

JavaScript scope controls where variables can be accessed.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

JavaScript uses scope to secure variables and prevent naming conflicts. If you create a variable inside a function, nothing outside that function can see or use it. This allows developers to use the same variable names in different places safely.

Example

Example
1/* this demonstrates scope */
2function myScope() {
3  let localScore = 50;
4  console.log(localScore); // Works
5}
6// console.log(localScore); // Error!

Key Points

  • Global scope variables are visible everywhere.
  • Function scope variables are hidden outside the function.
  • Block scope (`let` and `const`) hides variables outside curly braces.
  • Variables declared without keywords become global automatically.

Up Next

Continue your journey with the next topic.

Go to JS Hoisting