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 a JavaScript variable

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

Share

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

This article will explain in detail what is the JavaScript variable, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Like other programmable languages, JavaScript has the concept of "variable". A "variable" can be thought of as a container with a name. Put the data in these containers, and then you can know the type of data by the name of the container.

It is worth noting that in JavaScript programming, a variable must be declared before it can be used.

First, recognize the JavaScript variable

1. Is a container that stores the value of the data

Project

XQuery y and z are variables

Var x = 5; var y = 6; var z = x + y; document.getElementById ("demo") [xss_clean] = z

two。 Just like algebra.

Var price1 = 5; var price2 = 6; var total = price1 + price2

In the above case, price1, price2, and total are all variables.

Code parsing:

In programming, as in algebra, we use variables (such as price) to store values, just as in algebra, we use variables (total = price1 + price2) in expressions.

II. JavaScript identifier

1. What identifier?

All JavaScript variables must have unique names, which are called identifiers.

Identifiers can be short names (such as: X and y) or more descriptive names (such as age, sum, totalVolume).

two。 Naming rules for identifiers

Names can contain letters, numbers, underscores, and dollar signs. It must start with a letter. It also starts with dollars and _.

Names are case sensitive (Y and y are different variables), and reserved words (such as JavaScript keywords) cannot be used as names.

Note: JavaScript identifiers are case sensitive.

Declare (create) the JavaScript variable

Create a variable in JavaScript, called a declared variable.

Declare a JavaScript variable using the var keyword.

Var carName

After declaration, the variable has no value. (it has undefined values in programming)

To assign a value to a variable, use the equal sign.

CarName = "Volvo"

You can also specify a value for a variable when you declare it.

Var carName = "Volvo"

Create the variable carName and assign it to "Volvo", and then "output" its value to a HTML paragraph of id= "demo".

Project

Create a variable, assign it a value, and display it:

Var carName = "Volvo"; document.getElementById ("demo") [xss_clean] = carName

1. One sentence, multivariable

Start the statement with var and separate the variables with commas.

Var person = "John Doe", carName = "Volvo", price = 200

A declaration can span multiple lines

Var person = "John Doe", carName = "Volvo", price = 200

two。 Variables with no value declaration, Value = undefined

Variables are often declared as having no value. The value must be calculated, or something that will be provided later, such as the user entering a variable that has no value declaration, will have the value undefined.

After executing the following statement, the value of the variable carName is undefined.

Var carName

4. JavaScript arithmetic operation

Like algebra, you can do arithmetic with JavaScript variables, using operators like = and +.

HTML Code:

Project JavaScript variable

The result of 2 + 3 + "5" is:

Var x = 2 + 3 + "5" / * replace statement * / document.getElementById ("demo") [xss_clean] = x

JSON (replace the statement in turn to achieve the effect)

Var x = 5 + 2 + 3

1. You can also add strings, but the strings will be concatenated.

Var x = "John" + "" + "Doe"

Var x = "5" + 2 + 3

two。 If you put a number in quotation marks, the rest will be treated as a string and concatenated.

Var x = 2 + 3 + "5"

About what is the JavaScript variable to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

*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