Lädt...


🔧 Understanding Scope and Scope Chain in JavaScript.


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Introduction

JavaScript is a popular programming language used to make websites interactive. When learning JavaScript, understanding "scope" and "scope chain" is very important. These concepts help you know where your variables and functions can be accessed or used. In this article, I'll explain what scope and scope chain are with simple examples.

What is Scope?

Scope in JavaScript refers to the area or environment where a variable or function is accessible. Think of it as a box or boundary within your code. There are mainly two types of scope in JavaScript: global scope and local scope.

Global Scope:

Variables or functions declared outside any function are in the global scope. They can be accessed from anywhere in the code.

const name = "Samuel";

function greet() {
  console.log("Hello, " + name);
}
greet(); // Outputs: Hello, Samuel
console.log(name); // Outputs: Samuel

Here, the variable "name" is in the global scope, so it can be used inside the greet function and outside of it.

Local Scope:

Variables or functions declared inside a function are in the local scope. They can only be accessed within that function.

function greet() {
  const name = "Ojerinde";
  console.log("Hello, " + name);
}
greet(); // Outputs: Hello, Ojerinde
console.log(name); // Error: name is not defined

In this example, name is a local variable inside the greet function. Trying to access it outside the function results in an error.

Keep in mind that variables declared anywhere with the var keyword will be available in the global scope. This is why it is recommended that you declare your variable with either let or const keyword to avoid unwanted manipulations of variables.

What is Scope Chain?

The scope chain in JavaScript is the order in which the JavaScript engine looks for variables. When you try to use a variable, JavaScript first looks in the local scope. If it doesn't find it there, it moves up to the next outer scope and keeps doing this until it reaches the global scope.

Example of Scope Chain:

const globalVariable = "I am global";

function outerFunction() {
  const outerVariable = "I am outer";

  function innerFunction() {
    const innerVariable = "I am inner";
    console.log(innerVariable); // Outputs: I am inner
    console.log(outerVariable); // Outputs: I am outer
    console.log(globalVariable); // Outputs: I am global
  }

  innerFunction();
  console.log(innerVariable ); // Error: innerVariable is not defined
}

outerFunction();
console.log(outerVariable); // Error: outerVariable is not defined

In this example, when innerFunction tries to access innerVariable , it finds it in its own local scope. For outerVariable, it goes one level up to outerFunction's scope. Finally, for globalVariable, it goes up again to the global scope. If a variable isn't found at any level, it results in an error.``

Conclusion

Understanding scope and scope chain is important for writing effective JavaScript code. Scope helps you manage where variables and functions can be accessed, and the scope chain helps the JavaScript engine find these variables. By knowing these concepts, you can avoid errors and write cleaner, more efficient code. Remember, practice makes perfect, so keep experimenting with these ideas in your own code!

...

🔧 Understanding Scope and Scope Chain in JavaScript.


📈 51.89 Punkte
🔧 Programmierung

🔧 Understanding the Scope Chain, Scope, and Lexical Environment in JavaScript


📈 51.89 Punkte
🔧 Programmierung

🔧 Understanding the Scope Chain in JavaScript🚀


📈 36.96 Punkte
🔧 Programmierung

🔧 Understanding Scope Chain in JavaScript


📈 36.96 Punkte
🔧 Programmierung

🔧 Scope in JavaScript – Global vs Local vs Block Scope Explained


📈 33.11 Punkte
🔧 Programmierung

🔧 Scopes And Scope Chain in JavaScript


📈 30.46 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Scope: Global and Local


📈 29.67 Punkte
🔧 Programmierung

🔧 Understanding Lexical Scope and Closure in JavaScript


📈 29.67 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Blocks and Scope


📈 29.67 Punkte
🔧 Programmierung

🔧 Understanding Lexical Scope in JavaScript ✅


📈 28 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Powerful Mechanism for Variable Scope


📈 28 Punkte
🔧 Programmierung

🔧 Understanding Lexical Scope in JavaScript ✅


📈 28 Punkte
🔧 Programmierung

🔧 Understanding Variable Scope in JavaScript 🌐


📈 28 Punkte
🔧 Programmierung

🔧 Understanding Nested Scope in Javascript: The Onion Concept as a Metaphor


📈 28 Punkte
🔧 Programmierung

🔧 Understanding scope in JavaScript


📈 28 Punkte
🔧 Programmierung

🔧 [JS] The top-level scope is NOT always the global scope


📈 26.54 Punkte
🔧 Programmierung

🔧 JavaScript Scope and Hoisting


📈 21.51 Punkte
🔧 Programmierung

🔧 Scope, Hoisting and Closures in Javascript


📈 21.51 Punkte
🔧 Programmierung

🔧 JavaScript variables and scope


📈 21.51 Punkte
🔧 Programmierung

🔧 Scope, Closures, and Hoisting in JavaScript – Explained with Code Examples


📈 21.51 Punkte
🔧 Programmierung

🔧 Hoisting, Lexical Scope, and Temporal Dead Zone in JavaScript


📈 21.51 Punkte
🔧 Programmierung

🔧 Day 13: Deep Dive into Object Properties, Getters & Setters, and Lexical Scope in JavaScript 🎉


📈 21.51 Punkte
🔧 Programmierung

🔧 Mastering Variables and Scope in JavaScript: A Developer's Guide


📈 21.51 Punkte
🔧 Programmierung

📰 Facebook Exec Admits 'No Real Understanding' for the Scope of Fake News


📈 21.43 Punkte
📰 IT Security Nachrichten

🔧 Understanding Kotlin's Scope Functions


📈 21.43 Punkte
🔧 Programmierung

🔧 Node.js vs. Browser: Understanding the Global Scope Battle


📈 21.43 Punkte
🔧 Programmierung

🔧 Understanding the Scope of Pattern Variables in Java


📈 21.43 Punkte
🔧 Programmierung

🎥 Scope in JavaScript - HTTP 203


📈 19.84 Punkte
🎥 Video | Youtube

🔧 Core JavaScript: Scope


📈 19.84 Punkte
🔧 Programmierung

🔧 Scope in JavaScript


📈 19.84 Punkte
🔧 Programmierung

🔧 🔎 What is Lexical Scope in JavaScript?


📈 19.84 Punkte
🔧 Programmierung

🔧 All About JavaScript Scope


📈 19.84 Punkte
🔧 Programmierung

🔧 JavaScript Lexical Scope Explained shortly


📈 19.84 Punkte
🔧 Programmierung

matomo