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!! 😆

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Yuri 🇰🇷

Yuri 🇰🇷 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @codingyuri

10 Jul
Open Source is a great way to improve your reputation as a dev, and practice your coding skills by reading other people's code and receiving feedback!
It's also a great way to give back to the community!

How to contribute?
A thread 🧵
#Coding #WomenWhoCode #webdevelopment
1⃣ Find a project in GitHub you're interested in!

Usually this is the first step. Finding a project that interests you will motivate and help you become an active contributor. But as a #CodeNewbie, or your first time contributing, your first approach might be a bit different👇
If you're a beginner, the difficulty of the issues might put you off from choosing a project you like (happened to me!). Don't worry about it, beginners can contribute to open source and add contributing experience to their resumes as well. 👇
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(