Data type of JScript

Source: Internet
Author: User
Tags array object comparison expression string variable type null valid
js|jscript| Data | data type

Jscript has three primary data types, two types of composite data, and two special data types.

The primary (basic) data types are:

    • String
    • Numerical
    • Boolean

The composite (reference) data type is:

    • Object
    • Array

Special data types are:

    • Null
    • Undefined

String data type

A string value is a series of 0 or 0 Unicode characters (letters, digits, and punctuation marks) that are queued together. The string data type is used to represent text in JScript. The script can contain string literals that are placed in a pair of matching single or double quotes. Strings can contain double quotes, either with single quotes or single quotes, and double quotes on both sides of the double quotation mark. The following is an example of a string:

"Happy am I; from care I’m free!"'"Avast, ye lubbers!" roared the technician.' "42"'c'

Note that there is no type in JScript that represents a single character (for example, C + + char). To represent a single character in Jscript, you should create a string that contains only one character. A string containing 0 characters ("") is a null (0-length) string.

Numeric data types

There is no difference between integer and floating-point values in JScript; a JScript value can be either of them (all the values within JScript are represented as floating-point values).

Integer value

Integer values can be positive integers, negative integers, and 0. Can be expressed in 10, 8, and 16. Most of the digits in Jscript are expressed in decimal notation. The prefix "0" indicates the 8-in integer value, which can contain only 0 to 7 digits. A number with a prefix of "0" that contains the number "8" or "9" is interpreted as a decimal number.

Plus prefix "0x" (0 and x| X) Represents a 16-in-integer value. Can contain numbers 0 through 9, and letters A through F (uppercase or lowercase). Use the letters A through F to represent a single digit of decimal 10 through 15. That means 0xF equals 15, while 0x10 equals 16.

Octal and hexadecimal numbers can be negative, but cannot have decimal places, and cannot be represented by scientific notation (exponent).

Floating-point value

A floating-point value is a number with a decimal part. It can also be expressed in scientific notation. This means that uppercase or lowercase "e" is used to represent 10 of the second party. The eight-byte IEEE754 floating-point standard that JScript uses numerically. This means that the maximum number can go to ±1.7976931348623157x10308, the smallest to ±5x10-324. Numbers starting with "0" and containing a decimal point are interpreted as decimal floating-point numbers.

Note that the number starting with "0x" or "00" and containing the decimal point will be an error. The following is an example of a number in Jscript.

Digital Description equivalent decimal number
.0001, 0.0001, 1e-4, 1.0e-4 Four equal floating-point numbers. 0.0001
3.45e2 Floating point numbers. 345
42 Integer. 42
0378 Integer. Although it appears to be a octal number (starting with 0), 8 is not a valid octal number, so it is a decimal number. 378
0377 Octal integer. Note that although it looks 1 smaller than the number above, the actual value is very different. 255
0.0001 Floating point numbers. Although it starts with 0, it is not a octal number because it has a decimal point. 0.0001
00.0001 Error. The two-0 start is represented as octal, but the octal number cannot have a decimal portion. N/A (compilation error)
0Xff A hexadecimal integer. 255
0x37cf A hexadecimal integer. 14287
0x3e7 A hexadecimal integer. Note that ' E ' is not considered an exponent. 999
0x3.45e2 Error. Hexadecimal numbers must not have a decimal part. N/A (compilation error)

In addition, JScript contains special value numbers. They are:

    • NaN (not a number). Used when doing mathematical operations on inappropriate data, such as strings or undefined values.
    • is infinitely large. In JScript, if a positive number is too large, use it to represent.
    • Negative infinity. In JScript, if a negative number is too large, use it to represent.
    • Positive 0 and minus 0. JScript distinguishes between positive 0 and negative 0.

Boolean data type

Although string and numeric types can have countless different values, the Boolean data type has only two values. They are literal true and false. A Boolean is a truth value that represents the validity of a state (indicating that the state is true or false).

Comparisons in scripts usually get a Boolean result. Consider the next line of Jscript code.

y = (x == 2000);

Here to compare whether the value of variable x is equal to the number 2000. If equal, the result of the comparison is a Boolean value of trueand assigns it to the variable Y. If x is unequal to 2000, the result of the comparison is a Boolean value of false.

Boolean values are especially useful in structural control. You can combine a comparison that directly creates a Boolean value with a statement that uses that Boolean value. Consider the following example of a JScript code.

if (x == 2000)    z = z + 1;else    x = x + 1;

When the Boolean value is true , the if/else statement in Jscript performs an action (so that z = z + 1), and another action is performed when the Boolean value is false (x = x + 1).

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

This may not be the desired result  as follows!

Note that the above code does not check that x is equal to Y+z because only an equal sign (Assignment) is used. Instead, the above code assigns y+z to the variable x, and then checks to see if the entire expression has a zero value. To check if x is equal to Y+z, use the following code.

This is different from the code above!

For more information about comparisons, see procedures for controlling your program .

Null data type

Data type NULL has only one value in Jscript: null. Keyword NULL cannot be used as the name of a function or variable.

A variable containing null contains either "No value" or "no object." In other words, the variable does not hold a valid number, String, Boolean, array, or object. You can clear the contents of a variable by assigning a null value to it.

Note that in Jscript, NULL is not equal to 0 (unlike in C and C + +). It should also be noted that the typeof operator in JScript reports that the null value is of type Object , not type null. This potential confusion is for backward compatibility.

Undefined data type

Return the undefined value as follows:

    • object property does not exist,
    • A variable was declared but never assigned a value.

Note You cannot test whether a variable exists by comparing it to undefined, although you can check whether its type is "undefined". In the following code example, suppose the programmer wants to test whether the variable x has been declared:

This method does not work. if (x == undefined)       This method does not work. The  string must be checked   "undefined" if (typeof(x) == undefined)      for certain operations  This method is useful if (typeof(x) == "undefined")      for some operations

Consider comparing the undefined value with null.

someObject.prop == null;

The result of the comparison is truewhen the following conditions

    • If the attribute 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:
Have attributes '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.