Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of JavaScript Global scope and function scope

Shulou Source: shulou.com Published: 2022-06-01 05:40:31 10月21日 Update

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!

Tags: Function function global instance code variable instance analysis analysis content local more knowledge practical successful learning and then position only dilemma reality situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Docker OPPO Reno Linux