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 variables in JScript

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

Share

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

This article mainly shows you "how to use JScript variables", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use JScript variables" this article.

In any programming language, quantify a concept with a piece of data.

In Jscript, a variable is the name of a given concept; it represents a given instantaneous value. When you use this variable, you actually use the data it represents. Show me an example:

NumberOfDaysLeft = EndDate? TodaysDate

The mechanical understanding is to use variables to store, get, and manipulate all the different values that appear in the script. Create meaningful variable names; make it easy for others to understand the script.

Variable declaration

The first time a variable appears in a script is in a declaration. Variables are set in memory the first time they are used, so that they can be referenced later in the script. Declare a variable before you use it. You can use the var keyword to declare variables.

Var count; / / single declaration.

Var count, amount, level; / / multiple declarations declared with a single var keyword.

Var count = 0, amount = 100; / / variable declaration and initialization in a statement.

If the variable is not initialized in the var statement, the variable automatically takes the Jscript value undefined. Although it is not safe, ignoring the var keyword in the declaration statement is a legitimate Jscript syntax. At this point, the Jscript interpreter gives the variable global scope visibility. When a variable is declared at the procedure level, it cannot be used in the global scope; in this case, the variable declaration must use the var keyword.

Variable naming

The variable name is an identifier. In Jscript, use identifiers to:

Named variable

Named function

Give the label of the loop.

Jscript is a case-sensitive language. So the variable name myCounter is different from the variable name mYCounter. The name of a variable can be any length. The following rules should be followed to create legal variable names:

The first character must be an ASCII letter (uppercase and lowercase) or an underscore (_). Note that the first character cannot be a number.

Subsequent characters must be letters, numbers, or underscores.

The variable name must not be a reserved word.

Here are some examples of legal variable names:

_ pagecount

Part9

Number_Items

Here are some examples of invalid variable names:

99Balloons / / cannot start with a number.

The Smith&Wesson / / and symbol (&) character is not valid for variable names.

When you want to declare a variable and initialize it, but you don't want to specify any special value, you can assign it to the Jscript value null. Let me show you an example.

Var bestAge = null

The value of var muchTooOld = 3 * bestAge; / / muchTooOld is 0.

If a variable is declared but not assigned to it, the variable exists and its value is an undefined Jscript value. Let me show you an example.

Var currentCount

The value of var finalCount = 1 * currentCount; / / finalCount is NaN because currentCount is undefined.

Note that the main difference between null and undefined in Jscript is that null operates like the number 0, while undefined operates like a special value NaN (not a number). Comparing the null value with the undefined value is always equal.

Variables can be declared and assigned without the var keyword. This is the implicit declaration.

NoStringAtAll = ""; / / implicitly declare the variable noStringAtAll.

You cannot use undeclared variables.

Var volume = length * width; / / error? Length and width do not exist.

Forced conversion

The Jscript interpreter can evaluate an action item only if it has the same data type in the expression. If an expression attempts to operate on two different data types, such as a number and a string, without casting, an error result will be produced. But in Jscript, the situation is different.

Jscript is a free-type language. Its variables do not have a predetermined type (as opposed to strongly typed languages such as C++). Instead, the types of Jscript variables correspond to the types of values they contain. The advantage of this operation is that the value can be treated as another type.

In Jscript, you can perform operations on different types of values without having to worry about exceptions from the Jscript interpreter. Instead, the Jscript interpreter automatically changes (casts) one of the data types to another, and then performs the operation. For example:

Calculation result

The addition of a numeric value to a string casts a numeric value into a string.

The addition of a Boolean value to a string casts a Boolean value into a string.

The addition of a numeric value and a Boolean value forces a Boolean value to a numeric value.

Consider the following example.

Var x = 2000; / / a number.

Var y = "Hello"; / / a string.

X = x + y; / / casts a number into a string.

[xss_clean] (x); / / output 2000Hello.

To explicitly convert a string to an integer, use the parseInt method. To explicitly convert a string to a number, use the parseFloat method. Note that the string is automatically converted to an equal number when comparing the size, but remains as a string during the addition (concatenation) operation.

The above is all the content of the article "how to use JScript variables". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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