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

Case Analysis of JavaScript Local variables

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

Share

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

This article introduces the relevant knowledge of "JavaScript local variable 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!

Function with return value

Sometimes we want the function to return the value to the place where it was called.

This can be done by using the declare statement.

When you use the declare statement, the function stops execution and returns the specified value.

Grammar

Function myFunction ()

{

Var Xero5

Return x

}

The above function returns a value of 5.

Note: the entire JavaScript does not stop execution, just functions. JavaScript will continue to execute the code from where the function is called.

The function call will be replaced by the return value:

VarmyVar=myFunction ()

The value of the myVar variable is 5, which is the value returned by the function "myFunction ()".

Even if you don't save it as a variable, you can use the return value:

Document.getElementById ("demo") [xss_clean] = myFunction ()

The innerHTML of the "demo" element will be 5, which is the value returned by the function "myFunction ()".

You can base the return value on the parameters passed to the function:

Example

Calculates the product of two numbers and returns the result:

Function myFunction (afort b)

{

Return aquib

}

Document.getElementById ("demo") [xss_clean] = myFunction (4prime3)

The innerHTML of the "demo" element will be:

twelve

You can also use the declare statement when you just want to exit the function. The return value is optional:

Function myFunction (afort b)

{

If (a > b)

{

Return

}

X=a+b

}

If an is greater than b, the above code exits the function and does not calculate the sum of an and b.

Local JavaScript variable

The variable declared inside the JavaScript function (using var) is a local variable, so it can only be accessed inside the function. The scope of this variable is local.

You can use a local variable with the same name in different functions, because only the function that has declared the variable can recognize it.

As soon as the function finishes running, the local variable is deleted.

Global JavaScript variable

The variable declared outside the function is a global variable that can be accessed by all scripts and functions on the web page.

Lifetime of JavaScript variable

The lifetime of JavaScript variables begins when they are declared.

Local variables are deleted after the function runs.

Global variables are deleted after the page is closed.

Assign values to undeclared JavaScript variables

If you assign a value to a variable that has not been declared, the variable is automatically treated as an attribute of window.

This sentence:

Carname= "Volvo"

A property of window, carname, will be declared.

A global variable created by assigning a value to an undeclared variable in non-strict mode is a configurable property of the global object and can be deleted.

Varvar1=1;// is not configurable global properties

Var2=2;// does not use var declaration, but can configure global properties

Console.log (this.var1); / / 1

Console.log (window.var1); / / 1

Console.log (window.var2); / / 2

Deletevar1;//false cannot be deleted

Console.log (var1); / / 1

Deletevar2

Console.log (deletevar2); / / true

Console.log (var2); / / the error reporting variable has been deleted and undefined

This is the end of the content of "instance Analysis of JavaScript Local variables". 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!

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: 297

*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