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

How to use strings, numbers, Boolean, arrays and objects in JavaScrip

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

Share

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

This article mainly introduces how to use strings, numbers, Boolean, arrays and objects in JavaScrip. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

JavaScript function

A function is a reusable block of code that is event-driven or executed when it is called.

Function myFunction () {alert ("Hello World!");} Click here function myFunction (name,job) {alert ("Welcome" + name + ", the" + job);}

Tip: the above function prompts "Welcome Bill Gates, the CEO" when the button is clicked.

Tip: the function is flexible, and you can call it with different parameters, which will give you different messages:

Click here.

Function with return value

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

This can be achieved by using the return statement.

When you use the return statement, the function stops execution and returns the specified value. The syntax is as follows:

Function myFunction () {var x5; return x;}

Tip: the above function returns a value of 5.

Tip: the entire JavaScript does not stop execution, only the function.

Tip: JavaScript will continue to execute the code from the place where the function is called. The function call will be replaced by the return value:

Var myVar=myFunction ()

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

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

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

Tip: the innerHTML of the "demo" element will become 5, which is the value returned by the function "myFunction ()".

Tip: you can make the return value based on the parameters passed to the function, such as calculating the product of two numbers and returning the result:

The function called in this example performs a calculation and returns the result:

Function myFunction (demo b) {return aforb;} document.getElementById ("demo") [xss_clean] = myFunction (4P3)

Tip: the innerHTML of the "demo" element will be: 12

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

Function myFunction (a return; b) {if (a > b) {return;} x=a+b}

Note: 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 declared as a global variable.

The following statement declares a global variable carname, even if it is executed within the function:

Carname= "Volvo"; these are all the contents of the article "how to use strings, numbers, Boolean, arrays, objects in JavaScrip". Thank you for reading! Hope to share the content to help you, more related knowledge, 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