Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define the scope and global scope of functions in HTML

Shulou Source: shulou.com Published: 2022-06-01 05:41:35 09月20日 Update

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!

Tags: Function global function variable object similarity code key keyword content example learning practical deeper interest practical situation simple operation method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Huawei Shulou Technology Redmi