JS:SCOPE

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Blocks & Scope

A block is the code found inside a set of curly braces {}. Blocks help group 1 or more statements together & serve as an important structural marker for code. A block of code could be a function i.e.: const logSkyColor = () => { let color = 'blue'; console.log(color); // blue }; (the function body is actually a block of code) block in an if statement: if (dusk) { let color = 'pink'; console.log(color); // pink };

Global Scope

In global scope, variables are declared outside of blocks. These variables are called global variables. Because global variables are not bound inside a block, they can be accessed by any code in the program, including code in blocks. i.e.: const color = 'blue' const returnSkyColor = () => { return color; // blue }; console.log(returnSkyColor()); // blue Even though the color variable is defined outside of the block, it can be accessed in the function block, giving it global scope. color can be accessed within the returnSkyColor function block.

Scope

Scope is the context in which our variables are declared. We think about scope in relation to blocks because variables can exist either outside of or within these blocks.

Scope Pollution

Scope pollution is when we have too many global variables that exist in the global namespace, or when we reuse variables across different scopes. Scope pollution makes it difficult to keep track of our different variables and sets us up for potential accidents. For example, globally scoped variables can collide with other variables that are more locally scoped, causing unexpected behavior in our code. Let's look at an example of scope pollution in practice so we know how to avoid it: let num = 50; const logNum = () => { num = 100; // Take note of this line of code console.log(num); }; logNum(); // Prints 100 console.log(num); // Prints 100 You'll notice: We have a variable num. Inside the function body of logNum(), we want to declare a new variable but forgot to use the let keyword. When we call logNum(), num gets reassigned to 100. The reassignment inside logNum() affects the global variable num. Even though the reassignment is allowed and we won't get an error, if we decided to use num later, we'll unknowingly use the new value of num. While it's important to know what global scope is, it's best practice to not define variables in the global scope.

Good Scoping

Tightly scoping your variables will greatly improve your code in several ways: It will make your code more legible since the blocks will organize your code into discrete sections. It makes your code more understandable since it clarifies which variables are associated with different parts of the program rather than having to keep track of them line after line! It's easier to maintain your code, since your code will be modular. It will save memory in your code because it will cease to exist after the block finishes running. Here's another example of how to use block scope, as defined within an if block: const logSkyColor = () => { const dusk = true; let color = 'blue'; if (dusk) { let color = 'pink'; console.log(color); // pink } console.log(color); // blue }; console.log(color); // ReferenceError Here, you'll notice: We create a variable dusk inside the logSkyColor() function. After the if statement, we define a new code block with the {} braces. Here we assign a new value to the variable color if the if statement is truthy. Within the if block, the color variable holds the value 'pink', though outside the if block, in the function body, the color variable holds the value 'blue'. While we use block scope, we still pollute our namespace by reusing the same variable name twice. A better practice would be to rename the variable inside the block. Block scope is a powerful tool in JavaScript, since it allows us to define variables with precision, and not pollute the global namespace. If a variable does not need to exist outside a block— it shouldn't!

Block Scope

When a variable is defined inside a block, it is only accessible to the code within the curly braces {}. We say that variable has block scope because it is only accessible to the lines of code within that block. Variables that are declared with block scope are known as local variables because they are only available to the code that is part of the same block. Block scope works like this: const logSkyColor = () => { let color = 'blue'; console.log(color); // blue }; logSkyColor(); // blue console.log(color); // ReferenceError You'll notice: We define a function logSkyColor(). Within the function, the color variable is only available within the curly braces of the function. If we try to log the same variable outside the function, throws a ReferenceError.


संबंधित स्टडी सेट्स

Mid term Intro to ECON (ECO 140-231) Summer class 2021

View Set

American Government Chapters 5-9 review

View Set

Poultry Nutrition Exam#2 Dr. Bailey

View Set

Genetics Final Price Summer 2015 UVU

View Set

AP Psych, Unit 1 prologue Vocabulary

View Set

Real Estate Express Chapter 29 ❤️

View Set