1. javascript
  2. /basics
  3. /scope

The Basics of JavaScript Scope

Introduction

JavaScript scope is a fundamental concept of the language. It determines the accessibility or visibility of variables, functions, and objects in some particular part of your code during runtime. We'll dive into the basics of JavaScript scope, its set of rules, and the different types of scope.

Global & Function Scope

A variable declared outside of any function or block is considered to be in the global scope. These variables are accessible from anywhere in the code, including inside functions.

let globalVariable = "I'm accessible from anywhere!";

function myFunction() {
  console.log(globalVariable); // Output: "I'm accessible from anywhere!"
}

On the other hand, a variable declared inside a function is only accessible within that function's scope. These variables are also referred to as local.

function myFunction() {
  let localVariable = "I'm only accessible within myFunction";
}
console.log(localVariable); // ReferenceError: localVariable is not defined

It's also possible to declare a variable without using the var, let, and const. These variables are considered global but they can produce unexpected behavior as we'll explain later on.

function myFunction() {
  noKeywordVariable = "I'm a global variable";
}
console.log(noKeywordVariable); // Output: "I'm a global variable"

Block Scope

In JavaScript, a block is defined as a piece of code surrounded by curly braces {}. Variables declared with the let and const keywords within a block are only accessible within that block and are referred to as block-scoped variables. The feature was introduced in ECMAScript 6 (ES6)

if (true) {
  let blockScopedVariable = "I'm only accessible within this block";
}
console.log(blockScopedVariable); // ReferenceError: blockScopedVariable is not defined

When we use variables declared with the var keyword within a block, they are still considered to be in the global or function scope.

if (true) {
  var notBlockScopedVariable = "I'm not block scoped";
}
console.log(notBlockScopedVariable); // Output: "I'm not block scoped"

Best Practices

When it comes to scope in JavaScript, there are a few best practices that beginners should keep in mind.

First and foremost, almost always use let or const to declare variables instead of var. As a rule of thumb, it will help you avoid unexpected behavior and make your code more readable.

Additionally, try to avoid using global variables as much as possible. Instead, use local variables declared within functions. This helps to keep the global scope clean and prevents naming conflicts.

Another practice beginners should be mindful of is variable shadowing. It occurs when a variable declared within a function or block has the same name as a variable in an outer scope. In this case, the inner variable will take precedence.

let name = 'John Doe';
function sayHi() {
  let name = 'Jane Doe';
  console.log(`Hi, ${name}!`); // Hi, Jane Doe!
}
console.log(name); //John Doe

Finally, always use curly braces {} to define blocks, even if there is only one statement. Even though there are cases where you can omit them, this will help you avoid mistakes and make your code more readable.

Conclusion

  • Scope is crucial for writing maintainable and efficient code.

  • Understanding the different types of scope, such as global, function, and block scope, is essential.

To avoid common mistakes and write cleaner code, it is important to follow best practices:

  • Almost always, use let or const to declare variables instead of var

  • Avoid using global variables as much as possible and use local variables declared within functions instead.

  • Be mindful of variable shadowing and use curly braces {} to define blocks.

As you continue to develop your skills, you'll find that understanding scope is essential to working with any programming language. Naturally, when you become skilled enough, you'll encounter even more nuanced cases that we aim to cover in our extensive, knowledge-base documentation and guides.