scope in js

Yoseob Shin·2022년 3월 29일
0

javascript

목록 보기
8/24
post-thumbnail
post-custom-banner

Scope

Scope defines where variables can be accessed or referenced. While some variables can be accessed from anywhere within a program, other variables may only be available in a specific context.

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.

Tip: You can think of scope like the view of the night sky from your window. Everyone who lives on the planet Earth is in the global scope of the stars. The stars are accessible globally. Meanwhile, if you live in a city, you may see the city skyline or the river. The skyline and river are only accessible locally in your city, but you can still see the stars that are available globally.

Block

  • A block is the code found inside a set of curly braces {}.
  • We already have seen blocks from using if and function.
  • Blocks help us group one or more statements together and serve as an important structural marker for our code.

Global scope

  • In global scope, variables are declared outside of blocks.
  • They can be accessed by any code in the program, including code in blocks.
const color = 'blue'; // <-- global 
 
const returnSkyColor = () => {
  return color; // blue 
};
 
console.log(returnSkyColor()); // blue

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.

  • or local variable.
const logSkyColor = () => {
  let color = 'blue'; 
  console.log(color); // blue 
};
 
logSkyColor(); // blue 
console.log(color); // ReferenceError 

Scope Pollution

Having too many global variables can cause problems in a program.

When you declare global variables, they go to the global namespace. The global namespace allows the variables to be accessible from anywhere in the program. These variables remain there until the program finishes which means our global namespace can fill up really quickly.

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 num = 50;
 
const logNum = () => {
  num = 100; // Take note of this line of code
  console.log(num);
};
 
logNum(); // Prints 100
// The reassignment inside logNum() affects the global variable num.
console.log(num); // Prints 100

It’s best practice to not define variables in the global scope.

Practice Good Scoping

Tightly scoping your variables will greatly improve your code in several ways:

  • It will make your code more legible(readable) 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. 메모리 관리
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
  • From above, 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.


Review

  • Scope is the idea in programming that some variables are accessible/inaccessible from other parts of the program.
  • Blocks are statements that exist within curly braces {}.
  • Global scope refers to the context within which variables are accessible to every part of the program.
  • Global variables are variables that exist within global scope.
  • Block scope refers to the context within which variables are accessible only within the block they are defined.
  • Local variables are variables that exist within block scope.
  • Global namespace is the space in our code that contains globally scoped information.
  • Scope pollution is when too many variables exist in a namespace or variable names are reused.
profile
coder for web development + noodler at programming synthesizers for sound design as hobbyist.
post-custom-banner

0개의 댓글