Case Analysis of JavaScript Global scope and function scope
This article introduces the relevant knowledge of "JavaScript global scope and function scope instance analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Before ES2015, JavaScript had only two types of scope: global scope and function scope
Global scope
Variables declared globally (outside the function) have global scope.
Example
Var carName = "porsche"
/ / the code here can use carName
Function myFunction () {
/ / the code here can also use carName
}
Global variables can be accessed anywhere in the JavaScript program.
Function scope
Variables declared locally (within a function) have function scope.
Example
/ / the code here cannot use carName
Function myFunction () {
Var carName = "porsche"
/ / code here CAN use carName
}
/ / the code here cannot use carName
Local variables can only be accessed within the function in which they are declared.
This is the end of "JavaScript global scope and function scope instance analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!