What is a variable scope? The visibility and accessibility of a variable is determined by its scope. JavaScript has three scopes: Global scope Local scope and Block scope Global Scope A variable that can be accessible everywhere in the script. Local Scope If you declare a variable inside a function are local to the function. They are called local variables. Output: I am from say I am from global Scope Chain If interpreter cannot find a variable in its local context, then it looks for it in parent contexts until it's found. This is called scope chain. Output: Hello I am message In the above script, the JavaScript performs the following: It looks up for the variable message in the current context (Functional Execution Context) of the say(). But it cannot find it. So it goes out of the function and looks in the outer execution context which is the global execution context. It found the message variable there. The way that JavaScript resolves a variable is by looking at in its c
Teach Me to Code is a blog for those starting to learn web development. It's for developers who want to learn about more advanced topics. And for developers who want to keep up with the latest tech.