DEV Community

Tiberius Mairura
Tiberius Mairura

Posted on

5 1

Scope in JavaScript

Scope refers to the current context of javascript code execution.

This is where values and expressions are available or "visible" and can be referenced by the running program.

Values not visible or not available in the current execution context will NOT be available for use by your currently executing code.

Although scope can seem an easy concept to understand, in practice it sometimes trips even seasoned developers.

NOTE: Scope is normally hierarchically nested such that the child scope can reference the parent scope but the parent CANNOT reference the child scope.

In JavaScript, there are three types of scope and these include:

  • The Global scope This is the default scope for any code that runs in script mode i.e everything runs line by line without any strict execution contexts
  • The Module scope This scope occurs when code runs in module mode
  • The Function scope As it suggests, this scope is created inside a function.

Variables and Scope

In addition to the 3 scopes above, when variables are declared with ES6 syntax, let and const, a different scope is created:

  • The Block scope This is the scope created within the body of a pair of curly brackets.

Note that the var keyword does not implement the block scope.

Function Scope Example

function functionScopeExample() {
  var name = "Anjette"; //=> available only within functioScopeExample() body

  console.log(name); //=> Anjette
}

functionScopeExample();

console.log(name); //=> ReferenceError: name is not defined
Enter fullscreen mode Exit fullscreen mode

In the above snippet, we are not able to reference the name because its scope is limited to the functionScopeExample body i.e function scope.

The variable name is however successfully referenced within the function scope.

Global Scope Example

Let us move the variable to a global scope:

var name = "Anjette"; //=> available throughout the script i.e global scope
function globalScopeExample() {
  console.log(name); //=> Anjette
}

globalScopeExample();

console.log(name); //=> Anjette
Enter fullscreen mode Exit fullscreen mode

In the above example, the variable has been declared in a global scope and thus it is available both inside the function and outside the function scope.

Block Scope Example

A block is defined by a pair of curly brackets.

{
  const name = "Anjette";
  // let name = "Anjette";
}
console.log(name); //=> ReferenceError: name is not defined
Enter fullscreen mode Exit fullscreen mode

By using const or let, as shown in the snippet above, the variable name cannot be accessed outside the block. It is only accessible inside the block where it has been declared.

With the var keyword, the behavior is different.

{
  var name = "Anjette";
}
console.log(name); //=> Anjette
Enter fullscreen mode Exit fullscreen mode

Although the variable in the above snippet is declared in a block, the var keyword makes still available outside the block.

Key Takeaways

  • Scope defeines the extent to which variables are visible in a program
  • There are 4 types of scopes in JS: global, function, module and, block.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay