Summary of JavaScript data type conversion methods

Source: Internet
Author: User

Convert to Boolean type

Use two times non-op (!) : Tiantai Yi Zhuang Metallurgy

1 !!5 ==> true

With the Boolean constructor:

1 newBoolean(5) == > true

The value is converted to a Boolean type of False:0,+0,-0,nan, "" (empty string), Undefined,null

In addition to the above values other values after conversion are true, which need to be specifically mentioned: "0", New Object (), function () {}

Convert to String type

Add the empty string "":

1 123 + """123"

Using string constructors:

1 newString(123) = "123".

Conversions that require special attention:

1 +0 ==> "0"
2 -0 ==> "0"
3 -Infinity ==>"-Infinity"
4 +Infinity ==>"+Infinity"
5 NaN ==> "NaN"
6 undefined ==> "undefined"
7 null==> "null"
8 newObject() ==> "[object Object]"
9 function(){} ==> "function(){}"
Convert to numeric type

Take positive (+), minus 0 (-0), multiply one, (* *), divide by one (/1), take negative (-, this gets the opposite value):

1 +"123"= 123
2 +true= 1

With the constructor number ();

1 newNumber("123") = 123

Several conversions that require special attention:

01 ""(空字符串) ==> 0
02 "010"==> 10
03 "0x10"(16进制) ==> 16
04 "-010"==> -10
05 "-0x10"==> NaN
06 undefined ==> NaN
07 null==> 0
08 true==> 1
09 false==> 0
10 newObject() ==> NaN
11 newfunction(){} ==> NaN
Implicit type conversions

binary addition operation (+): If one of the two operands is of type string, the two operands are converted to a string type and then added. If there are no string types in the two operands, then the two operands are converted to numeric types and then the operations are done.

1 "123"+123 = "123123";
2 123+123 = 246
3 truetrue= 2
4 true+ undefined = NaN (因为undefined转换为数值为NaN,所有结果为NaN)
5 truenull= 1 (null转换为数字是0)
6 "123"null "123null"(包含字符串将转化为字符串相加)
7 "123"+ undefined = "123undefined"(undefined同样适用)
8 1 + 2 + "123""3123"(1+2是首先按照数值型计算的)

binary minus multiplication operations (-*/): Because only numeric types have a-*/operation, the two operands are converted to numeric and then calculated.

1 "123"-123 = 0

Unary take positive (+), take negative operator (-): take positive or negative are for the numerical type of operation, so the operand will be converted to numeric type and then do the operation.

1 +"123"= 123
2 -"123"= -123
3 +"123e"= NaN
4 +"123f"= NaN
5 +"123e-2"= 1.23

One Yuan non (!) Operator: A non-operator needs to convert the operand to a Boolean type.

1 !"123"false
2 !!"123"true
3 !123 = false
4 !!123 = true

logical operators (&&) and (| | ):

In && or | | The sides are judged by the Boolean type to judge, but I found an interesting thing when I was testing.

&& operator: If an item is false, the expression returns false, and if none of the entries are false, the expression returns the original value of the rightmost item.

123 && && 45 returns 45, not the true we imagined. So if there is 123 && = True Then it should be false. As to if (123 && 23) It is true that it should be converted to a Boolean type of 23.

|| Operator: to | | The results of the test are not the same as I imagined, | | Returns a value that is not false after the first conversion, and if it is false, it returns the last value that is false (the value before the type conversion).

Example: 123 | | 23 returns 123, not the imagined True.false | | NULL returns NULL instead of imaginary false.

Type conversion function

Parsefloat converted to floating point number:

The character resolution function obtains each character until it encounters a character that is not a numeric value, and then returns the numeric value it has obtained. A few special notes to be taken:

1 ""(空字符串) ==> NaN
2 "123e-2"== > 1.23 (科学计算法是可以识别的)
3 "010"==> 10 (8进制不能识别)
4 "0x10"==> 0 (16进制不识别)
5 "-010"==> -10
6 null,undefined,true,false,newObject(),function(){} ==> NaN

parseint Convert to signed integer:

Similar to parsefloat, but he will discard the decimal places (note that it is not rounded, is completely discarded, as is done with Math.floor), and that he can identify octal and 16 binary representations:

1 123e-2 == > 1
2 "123e-2"==> 123
3 "010"== > 8
4 "0x10"==> 16
5 "-010"== > -8
6 "-0x10"==> -16
7 null,undefined,true,false,newObject(),function(){},-Infinity +Infinity NaN ==> NaN

The difference between the three rounding functions:

Math.ceil (): "Ceiling", very image, right? is to take the smallest integer greater than or equal to the parameter.

1 8.7 == > 9
2 -8.7==> -8

Math.floor (): "Floor", take the smallest integer less than or equal to the parameter.

1 8.7 ==> 8
2 -8.7 ==> -9

Math.Round (): "Rounding" takes an integer.

Summary of JavaScript data type conversion methods

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.