JScript Data Type

Source: Internet
Author: User
Tags type null


JScript has three main data types, two composite data types, and two special data types.

The main (basic) data types are:

String
Value
Boolean
The composite (reference) data type is:

Object
Array
The special data types are:

Null
Undefined
String Data Type
A string value is a string of zero or more Unicode characters (letters, numbers, and punctuation marks ). The string data type is used to represent the text in JScript. The script can contain string text, which is placed in a pair

Enclosed in single or double quotation marks. A string can contain double quotation marks. The double quotation marks must be enclosed in single quotation marks or single quotation marks. The double quotation marks must be enclosed in double quotation marks. The following is a string example:

"Happy am I; from care I'm free! "
'"Avast, ye lubbers! "Roared the technician .'
"42"
'C'
Note that JScript does not indicate the type of a single character (such as C ++ char ). To represent a single character in JScript, you should create a string containing only one character. A string containing zero characters ("") is a null (zero-length) string.

.

Numeric data type
In JScript, there is no difference between integers and floating-point values. JScript values can be any of them (jscript internally expresses all values as floating-point values ).

Integer value
Integer values can be positive integers, negative integers, and 0. It can be represented in hexadecimal, octal, and hexadecimal. In JScript, most numbers are represented in decimal format. The prefix "0" indicates the integer value in octal format. It can only contain numbers ranging from 0 to 7. Before

The number that is suffixed with "0" and contains numbers "8" or "9" is interpreted as a decimal number.

The prefix "0x" (zero sum x | x) indicates the hexadecimal integer value. It can contain numbers 0 to 9 and letters A to F (uppercase or lowercase ). Use letters A to F to indicate a single number ranging from 10 to 15. That is to say, 0xf is equal to 15, and

0x10 equals 16.

The octal and hexadecimal numbers can be negative, but cannot have decimal places. They cannot be expressed by scientific notation (exponent.

Floating point value
The floating point value is the number of digits with decimal digits. It can also be expressed by scientific notation. That is to say, the upper or lower case "E" is used to represent the power of 10. The eight-character section ieee754 floating point standard represented by a numerical value in JScript. This means the maximum number is allowed.

To ± 1. 7976931348623157x10308, minimum to ± 5x10-324. The number that starts with "0" and contains the decimal point is interpreted as a decimal floating point number.

Note that the number of decimal points starting with "0x" or "00" is incorrect. The following is an example of numbers in JScript.

Number Description in decimal format
. 0001, 0.0001, 1e-4, 1.0e-4 four equal floating point numbers. 0.0001
3.45e2 floating point number. 345
42 integer. 42
An integer of 0378. Although it looks like the octal number (starting with 0), 8 is not a valid octal number, so it is a decimal number. 378
0377 octal integer. Note that it seems to be 1 less than the above number, but the actual value is quite different. 255
0.0001 floating point number. Although it starts with zero, it is not an octal value because it has a decimal point. 0.0001
00.0001 error. The two zeros start with octal, but the Octal numbers cannot contain decimal parts. N/A (compilation error)
0xff hexadecimal integer. 255
0x37cf hexadecimal integer. 14287
0x3e7 hexadecimal integer. Note that 'E' is not considered an index. 999
0x3. 45e2 error. The hexadecimal number cannot have decimals. N/A (compilation error)

In addition, JScript contains special values. They are:

Nan (not a number ). Used for mathematical operations on inappropriate data, such as strings or undefined values.
Positive infinity. In JScript, if a positive number is too large, it is used for representation.
Negative infinity. In JScript, if a negative number is too large, it is used for representation.
Positive 0 and negative 0. JScript distinguishes between positive 0 and negative 0.
Boolean data type
Although the string and number types can have countless different values, the boolean data type has only two values. They are true and false characters. A boolean value is a true value that indicates the validity of a State (indicating that the State is true or false ).

The comparison in the script usually produces a boolean result. Consider the next line of JScriptCode.

Y = (x = 2000 );
Here we need to compare whether the value of variable X is equal to the number 2000. If the values are equal, the comparison result is a Boolean value of true and assigned to the variable Y. If the values of x and 2000 are different, the comparison result is a Boolean value of false.

Boolean value is particularly useful in structure control. You can combine the comparison of directly created Boolean values with statements using this Boolean value. Consider the following sample JScript code.

If (x = 2000)
Z = z + 1;
Else
X = x + 1;
When the Boolean value is true, the IF/else statement in JScript executes an operation (in this case, Z = z + 1 ), when the Boolean value is false, another operation is executed (x = x + 1 ).

You can use any expression for comparison. Any expression with a value of 0, null, undefined, or null string is interpreted as false. The expression of any other value is interpreted as true. For example, you can use the following expression:

If (x = Y + Z) // This may not be the expected result-as follows!
Note that the code above does not check whether X is equal to Y + Z, because only one equal sign (Value assignment) is used ). On the contrary, the code above assigns y + Z to the variable X, and then checks whether the value of the entire expression is zero. Check whether X is equal to Y + Z to make

Use the following code.

If (x = Y + Z) // This is different from the above Code!
For more information, see ControlProgram.

NULL data type
In JScript, the Data Type null has only one value: null. The null keyword cannot be used as the name of a function or variable.

A variable that contains null contains "no value" or "no object ". In other words, the variable does not save valid numbers, strings, Boolean, arrays, or objects. You can assign a null value to a variable to clear the content of the variable.

Note that in JScript, null and 0 are not equal (different from C and C ++ ). It should also be noted that the typeof operator in JScript reports the null value as the object type, rather than the type null. This potential obfuscation is

Backward compatibility.

Undefined Data Type
The undefined value is returned as follows:

The object property does not exist,
Declared variables but never assigned values.
Note that you cannot compare it with undefined to test whether a variable exists, although you can check whether its type is "undefined ". In the following code example, assume that the programmer wants to test whether the variable X has been declared:

// This method does not work
If (x = undefined)
// Perform some operations

// This method also does not work-check required
// String "undefined"
If (typeof (x) = undefined)
// Perform some operations

// This method is valid
If (typeof (x) = "undefined ")
// Perform some operations
Consider comparing the undefined value with null.

Someobject. Prop = NULL;
In the following cases, the comparison result is true,

If the property someobject. Prop contains a null value,
If the property someobject. Prop does not exist.
To check whether an object property exists, you can use the new in OPERATOR:
If ("prop" in someobject)
// Someobject has the property 'prop'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.