yuri 🇰🇷 Profile picture
devrel | technical writer, self-taught coding person | idk what im doing 💫on a twitter break💫

Aug 2, 2021, 12 tweets

WTF is the deal with scope?😤

Scope can be very confusing when you're just starting out, especially with variable declarations!! So, I think that it's an important concept to know early on!😆

In this thread, we'll review scopes & its weird parts😉

A thread 🧵

1⃣WTF is scope?

Scope: visibility of the variable

Basically, scope refers to how your program can see or access a variable. Normally, when you declare a variable, It has global scope, meaning that it's accessible anywhere on your code.

2⃣ Variable global scope

Variables with global scope are declared OUTSIDE a function or a loop. If a variable's scope is global, it can be accessed anywhere in the code, even inside other functions or loops.

3⃣Variable local scope

On the other hand, a local scope variable is created INSIDE a function or loop. They cannot be seen nor used outside of that function/loop.

TDLR; you can only use that variable inside that function/loop.

4⃣Function global scope

Functions also have scope, not only variables. But, it's the same concept as a variable.

Normally, functions have global scope, so they're accessible anywhere in the code.

5⃣Function local scope

Functions have local scope too. When you nest a function, the child function is only seen within the parent function. The child function cannot be seen outside the parent.

6⃣Variable block scope

Another kind of scope is the block scope, which was introduced by ES6.

Generally, anything inside curly {} brackets is a block. It only works with keywords `let` and `const`, but not `var`.

Variables declared inside {} can only be seen inside that block

7⃣`var`

Block scope doesn't work with `var`. Variables declared with `var` are either function scope or global scope, but they're visible through blocks.

In other words, `var` acts like let/const inside a function, but it's global scoped with loops/conditionals/blocks.

8⃣`var` nested loop/function scope

Another thing you should know is that declaring a variable with `var` inside a child loop that's nested inside a parent function will be accessible to the whole function, but not outside that function.

The above is possible because of scope with `var`.

Remember that `var` behaves like global scope with if...else/loops/blocks, but it does have function scope.

On the other hand, remember that `let` does have if...else/loop/block/function scope, so if you declare the above with `let` instead, it'll be only accessible inside that loop

FINALLY we finished this thread!! It was quite long, but thank you for making it this far!!

I had trouble understanding scope when I first started, so I hope this helps y'all too😆

Follow me @codingyuri for more threads about difficult bullshit!! 😆

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling