Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the validity period of the JavaScript variable?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what is the validity period of the JavaScript variable", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the validity period of the JavaScript variable" can help you solve your doubts.

In JavaScript, objects and functions are also variables.

Scope determines the accessibility of variables, objects, and functions from different parts of the code.

Automatic global

If you assign a value to a variable that has not been declared, the variable automatically becomes a global variable.

This code declares a global variable carName, even if it is assigned within the function.

Example

MyFunction ()

/ / the code here can use the carName variable

Function myFunction () {

CarName = "porsche"

}

Strict mode

All modern browsers support running JavaScript in "strict mode".

You will learn more about how to use strict mode later in this tutorial.

Global variables are not automatically created in strict mode.

Global variables in HTML

With JavaScript, the global scope forms a complete JavaScript environment.

In HTML, the global scope is window. All global variables belong to the window object.

Example

Var carName = "porsche"

/ / the code here can use window.carName

Warning

Do not create global variables unless you intend to do so.

Your global variable (or function) can override the window variable (or function).

Any function, including the window object, can override your global variables and functions.

Validity period of JavaScript variable

The validity of the JavaScript variable begins when it is created.

Local variables are deleted when the function is complete.

The global variable will be deleted when you close the page.

Function parameter

Function parameters are also local variables within the function.

After reading this, the article "what is the validity period of JavaScript variables" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report