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
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 🤯