How to define the scope and global scope of functions in HTML
This article mainly explains "how to define the scope and global scope of functions in HTML". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to define the scope and global scope of functions in HTML.
Function scope
When declaring variables within a function, using var is very similar to let.
They all have function scope:
Function myFunction () {
Var carName = "porsche"; / / function scope
}
Function myFunction () {
Let carName = "porsche"; / / function scope
}
Global scope
If you declare a declaration outside a block, then var and let are similar.
They all have a global scope:
Var x = 10; / / Global scope
Let y = 6; / / Global scope
Global variables in HTML
In the case of JavaScript, the global scope is the JavaScript environment.
In HTML, the global scope is the window object.
Global variables defined by the var keyword belong to the window object:
Example
Var carName = "porsche"
/ / the code here can use window.carName
The global variable defined by the let keyword does not belong to the window object:
Example
Let carName = "porsche"
/ / the code here cannot use window.carName
At this point, I believe you have a deeper understanding of "how to define the scope and global scope of functions in HTML". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!