Javascript Function Scope (or scope in general)
Scope is an important concept in coding.
“When you create a function, the variables and other things defined inside the function are inside their own separate scope, meaning they are locked away in their own separate compartments, unreachable from inside other functions OR from code outside the functions.
The top-level outside all your functions is called the global scope. Values defined in the global scope are accessible from EVERYWHERE in the code.” (Source- MDN Webdocs).
We have this hierarchy of access because it allows for security and organization.
MDN Webdocs using the example of a zoo. Different animals are kept in different habitats. If those animals were placed (or called like in Javascript) into different habitats, it wouldn’t work. Your job is a zookeeper who keeps everything straight.
Resources:
Video: Javascript Scope (Local vs Global) by Mosh
Article with exercises: MDN Webdocs - Functions
Article with useful examples: Understanding Javascript Scope by Alexander Kondov.














