JavaScript data type Learn notes _javascript tips

Source: Internet
Author: User
Tags numeric

There are 5 simple data types in ECMAscript, also known as basic data types: Undefined, Null, Boolean, number, and string. There is also a complex data type--object.

Undefined type

When declaring a variable with VAR but not initializing it, the value of the variable is undefined. Such as:

var number;
document.write (number); Undefined

If you do not declare a variable, the following error occurs. Such as:

document.write (str); Error

However, when executed with typeof, the undefined value is returned with or without a declaration. Such as:

var num;
document.write (typeof num); Undefined
document.write (typeof str);//undefined

Null type

"Object" is returned when using typeof to detect null. Such as:

var num = null;
document.write (typeof num); Object

The undefined values are actually derived from null values, so their equality returns true. Such as:

Alert (Null = = undefined); True

It should be noted here that as long as the variable that is intended to save the object has not actually saved the object, you should explicitly let the variable save the null value, which helps distinguish between null and undefined. Such as:

var num1 = null;
var num2;
document.write (typeof Num1 + "" + typeof num2); Object undefined

Boolean type

To convert a value to its corresponding Boolean value, you can use the Transformation function Boolean (). Such as:

var str = "HelloWorld";
document.write (Boolean (str)); True

A table of various data types and their corresponding conversion rules is given in the 3.4.4 in the book "JavaScript Advanced Programming design." There are probably the following:

    • Any non-empty string is converted to true when the data type is string;
    • When the data type is number, all but 0 and nan are converted to false, and others are converted to true;
    • When the data type is object, all but null are converted to true;
    • When the data type is undefined, n/A will be converted to true,undefined to be converted to false;

Note here that the IF statement often uses a Boolean conversion that automatically executes the response as a condition. Such as:

var str = "HelloWorld";
if (str) {
  document.write ("Hellothere");
}//hellothere

Number Type

In addition to being decimal, integers can also be represented by octal or hexadecimal literals, where the first digit of the octal literal must be 0, followed by the octal number sequence (0~7). Such as:

var num1 = 070; Octal
var num2 = 032;//Eight binary
var num3 = 079;//Wrong Octal (>7)
var num4 = 09;//Wrong octal (>7)

The first two digits of the hexadecimal literal must be 0x, followed by any hexadecimal number (0~9 and a~f). Such as:

var num1 = 0xA; Hexadecimal
var num1 = 0xa;//also hexadecimal 10 (letter case-insensitive)

Floating-point values

Note here that you should never compare a specific floating-point number. Such as:

var a = 0.1;
var b = 0.2;
if (A + b = = 0.3) {
  document.write ("You are Right")
}//Because 0.1 plus 0.2 is actually equal to 0.30000000000000004

Range of values

The maximum and minimum values that ECMAScript can represent are stored in Number.MAX_VALUE and Number.min_value. To determine whether a number is poor, you can use the Isfinite () function. Such as:

document.write (isfinite (Number.MAX_VALUE + number.max_value)); False

NaN

0 divided by 0 returns Nan, and a positive number divided by 0 returns Infinity, and the plural returns-infinity. Second, NaN is not equal to any number, including itself. Another function isNaN () can help us determine whether this parameter is "not numeric". Such as:

document.write (isNaN ("a")); true;
document.write (isNaN ("324")); false;
document.write (isNaN (true)); false;
document.write (isNaN (false)); false;
document.write (isNaN (NaN)); True

numeric conversions

There are three functions that convert non-numeric values to numeric values: Number (), parseint (), parsefloat ().

The conversion rules for number () are as follows:

If this is a Boolean value, True and false are converted to 1 and 0.
If it is a numeric value, it does not change.
If it is a null value, it is converted to 0.
If it is undefined, return nan.
If it is a string, follow these rules:

    • Converts to a decimal value if there is only a number.
    • If it is a floating-point format, it is converted to the corresponding floating-point number. Leading zeros are also ignored.
    • If it is in hexadecimal format, it is converted to a decimal number.
    • If the string is empty, it is converted to 0.
    • Other conditions are converted to Nan.

Look at the following example:

document.write (number (true)); 1
document.write (number (false));//0
document.write (number ("789"))//789
document.write ( NULL)); 0
document.write (number (undefined));//nan
document.write (number ("02.0942"));//2.0942
document.write (number (0xa));
document.write (Number ("")); 0
document.write ("Fdsa");//nan

The conversion rules for parseint () are as follows:

    • If the first character is not a numeric character or symbol, parseint () returns Nan.
    • Converting an empty string with parseint () returns Nan.
    • If the first character is a numeric character, it continues to parse the second character until it encounters a non-numeric character.

Here are the specific examples:

document.write (parseint ("FDS")); NaN
document.write (parseint ("")); NaN
document.write (parseint ("1111112ABC"));//1111112
document.write (parseint (" -1111112ABC"));//- 1111112
document.write (parseint ("+1111112ABC"));//-1111112
document.write ("0xa"); parseint
document.write (parseint ("0022.00009"));
document.write (parseint ("070"));//ecmascript 3 is considered to be 56 (octal), ECMAScript 5 is considered 70 (decimal)

Also note that ECMAScript 5 does not have the ability to parse octal values, so to eliminate this problem, you can provide a second argument for this function: the cardinality (how many) to use when converting, as follows:

document.write (parseint ("070", 10));
document.write (parseint ("070", 8));//56
document.write ("070", 16); parseint

In most cases, it is best to set the default to 10.

The conversion rules for parsefloat () are as follows:

    • Similar to parseint (), the first decimal point of the string is valid, and the second decimal point, starting with the second decimal point, is not valid.
    • He can't parse hexadecimal values!!!
    • He can only parse decimal values!!!
    • He did not specify the usage of the system with a second cardinal number.

Here are the specific examples:

document.write (parsefloat ("421")); 421
document.write (parsefloat ("0421.32.1"));//421.32
document.write ("0xaafd"); parsefloat
document.write (parsefloat ("070"));//70
document.write (parsefloat ("070ABC"));//70
document.write (Parsefloat ("")); NaN
document.write (parsefloat ("abc"));//nan

String type

There are two ways to convert a value to a string. The first is the ToString () method that uses almost every value. As follows:

document.write (()). ToString (10)); "Document.Write" (
(0xa). ToString);//"
document.write" ((0xa). ToString (2));//"1010"
document.write ((True). ToString (10)); "True"
document.write ((false). toString);//"false"

Also note that null and undefined cannot be converted.

document.write ((null). ToString (10));
document.write ((undefined). toString (10));//

If you do not know whether the value you want to convert is null or undefined, you should use the transformation function string (), and if it is null, return "NULL" if it is undefined return "undefined". As follows:

document.write (String (null)); "Null"
document.write (String (undefined));//"Undefined"

In addition, the object type is described in more detail in the next article.

The above is a brief introduction to JavaScript data types, which I hope will help you learn about JavaScript data types.

Related Article

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.