Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Editing and Filtering Names States of the Application

Scope related question, return explanation

Question 1) In this video, Guil took some variables from within the second if statement and moved it to the first if statement. I don't understand how many scopes there are and how do we know that we can retrieve a variable from a function, how can I know if I can get a variable from the global scope, what if the if statement is inside other 5 if statements, does that affect the way that I am going to retrieve a variable from the global scope? Question 2) I saw the documentation related to the return function but I still do not understand how it works, I feel there is more to it can someone enlighten me?

Thank you very much for your effort and I really appreciate it, thank you!

2 Answers

Evgeniia Mas
Evgeniia Mas
4,452 Points

I don’t remember this video in all details and my access is paused now, but this topic seemed quite complicated to me too. But I think it is better to have some real situation in your mind to get it right!

Let's submit you and your family have the house, which is opposite of mine. You have a TV set and all your family members, having the keys to the door, can watch it at any time. Even if you (being mean, for example) decide to move it from living room into the bathroom - all your family members will be able to watch it, as they have free access to everything inside of the house.

As to us, unfortunately, we have no TV set, but there is a washing machine in our house and also all the members of my family, having keys, can use it.

And the members of your family don't have keys to my house and therefore can’t use a washing machine, as we can't watch our favorite movies without TV.

We are not friends and (for this example not very kind people): I don’t allow you to enter my house even if you have a huge amount of dirty laundry and you won't let me in even if watching a show is one of the biggest dream of my life.

Between our houses there is a shop. It is common, therefore everybody can go there at any time and buy something.

And finally, back to our “dear” javascript functions. The shop is “global”, as everyone can get there and use it, not only we, but the all people of our city (as global variable outside the function). TV set is local within your house (function HOUSE1) if only you and members of your family have an access to them. And also a washing machine within my house (another function HOUSE2 with it’s variables and methods).

To tell the truth, I don't even know that you have TV, because I never was in your house, as well as local variables are not accessible outside the function and one function doesn’t know what is in another one if it has no “key”.

If you want to watch a football match, I don’t think you’ll run to the “global” shop to buy a new TV, you just turn on your own also as functions do it. Only if it is broken (no such variable) you will look for a new one – variable in global scope.

English is not my mother tongue and I am not sure I’ll use right word, but as I understand in js within the for, while, if (important- unlike function declarations!!!) { brackets are rather “decorative”.

The same way as your family members inside of your house have no problems, run and watch TV set and no matter it is in bathroom (room as another block inside of same function HOUSE1) now and not at it’s previous place.

But if without jokes, at least in English I found pretty good the book series with the provocative title “You don’t know JS”, in Internet it is for free. Here is a link to scope and closure part.

https://github.com/getify/You-Dont-Know-JS/tree/master/scope%20%26%20closures

And also new "let" in current ES6 standard solves problems of "var" giving the block-scoped declarations. You can read it in that books too.

I am sorry that I didn't thank you earlier but I was very busy, thank you very much for all that time that you put into explaining my problem to me. I figured it out after a while but I am pretty sure you made it even clearer. Thank you very much buddy I hope you have a nice day ❤️

Evgeniia Mas
Evgeniia Mas
4,452 Points

Thanks for your feedback) It's ok, I didn't expect anything, I just wanted to give you a hand. And it is nice that your worked on this topic yourself. It is known: than more efforts - than better result))! Happy learning!

Steven Parker
Steven Parker
229,744 Points

Remember that scope works "outside in".

So any variable in the global scope will be available to inner scopes, no matter how many layers of nesting. The only concern would be if an inner scope were to use the same name again for something else — this would "shadow" or hide the global variable.

As for question2, return is not a function but a keyword that is used to both end the current function and to identify the value that will be passed back to the code which called the function. Can you be more specific about what is confusing you?

Here you are again :) Thank you again for replying. Well, I don't really understand what I don't understand but anyways I will do some research and I am pretty sure that I will figure it out. Maybe I watched some sort of video on youtube that got me confused in the first place. Again thank you buddy