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 Asynchronous Programming with JavaScript Understanding Promises Reject a Promise and Handle Errors

How come the variable out side of the scope is being captured inside of if condition?

Hey folks! Sorry if this is a dumb question but isn't the function (and obviously the 'if' block) scoped? Then how come the order variable out side of the scope is captured inside of if condition? I feel it magical to see that if condition is getting it's condition validated by a variable outside of the whole block. are they two entirely different entities?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Mohammed Zeeshan! Personally, I like to think of JS functions like cars with heavily tinted windows. They can see things outside of themselves: people, stores, the streets, light posts, trees, and even other cars (functions). But nothing can see inside those cars. So anything defined inside a function will not be accessible by other functions or out in the "global" scope. But the functions have access to things defined outside themselves.

So no, those are not two separate entities. The function has access to things (in this case a variable), that was defined outside itself.

Hope this helps! :sparkles:

Thanks so much Jennifer Nordell . That was an excellent daily life example and instantly got the point. But it still feel magical for me. Programming is amazing and i'm mind blown 🤯