Summary of JavaScript type conversion methods and issues needing attention (quite comprehensive)

Source: Internet
Author: User

I. Method of type conversion and precautions:
1.Convert to boolean:
(1) Using two non-operations(!) :
!! 5 ==> true
(2) Use a Boolean constructor:
New Boolean (5) ==> true
Convert value to boolean typeFalse:
0,+ 0,-0,NaN,""(Null String), Undefined, null
Other values except the preceding values are convertedTrueWhich of the following is important?:
"0", new Object (), function (){}
2.Convert to string type:
(1) Add a Null String"":
123 + "" = "123"
(2) String constructor:
New String (123) = "123 ".
Conversions that require special attention:
+ 0 => "0"
-0 => "0"
-Infinity => "-Infinity"
+ Infinity => "+ Infinity"
NaN => "NaN"
Undefined => "undefined"
Null => "null"
New Object () => "[object Object]"
Function () {}=> "function (){}"
3.Convert to numeric type:
(1) Get positive (+), Minus Zero (-0), Multiply by one ,(* 1), Divided by one (/1 ),Take negative (-).
+ "123" = 123
+ True = 1
(2) ConstructorNumber ();
New Number ("123") = 123
Several conversions that require special attention:
""(Null String)==> 0
"010" => 10
"0x10" (16Hexadecimal) => 16
"-010" =>-10
"-0x10" => NaN
Undefined ==> NaN
Null => 0
True => 1
False => 0
New Object () => NaN
New function () {}=> NaN

Ii. implicit type conversion:
(1)Binary addition (+): If one of the two operands isStringType, which converts two operandsStringType.
If the two operands do not have the string type, the two operands are converted to the numeric type before the operation.
Example:
"123" + 123 = "123123 ";
123 + 123 = 246
True + true = 2
True + undefined = NaN(BecauseUndefinedConvert to numeric valueNaN,All results areNaN)
True + null = 1 (nullConvert to number is0)
"123" + null = "123 null "(Adding a string)
"123" + undefined = "123 undefined"(Undefined)
1 + 2 + "123" = "3123" (1 + 2First, it is calculated based on the numeric type.)
(2)Binary subtraction, multiplication, division (-*/):Because only the value type is available-*/Therefore, the two operands are converted to the numeric type before the operation.
"123"-123 = 0
(3)One dollar positive (+), Take the negative operator(-):Both positive and negative operations are numeric operations. Therefore, the operands are converted to numeric operations.
+ "123" = 123
-"123" =-123
+ "123e" = NaN
+ "123f" = NaN
+ "123e-2" = 1.23
(4)Non-RMB(!)Operator:Non-operators need to convert the operands to the boolean type.
! "123" = false
!! "123" = true
! 123 = false
!! 123 = true
(5)Logical operators (&&) And (|):
In&&Or|When both sides are judged, they are converted to boolean type for judgment,
But I found an interesting thing during the test.
&&Operator: If one item isFalse, Then the expression returnsFalse,If none of the items areFalse, Then the expression returns the original value of the rightmost item.
Example:
123 & 23 & 45Return45,Instead of what we thinkTrue.
So if123 & 23 = trueSo it should beFalse.
AsIf (123 & 23)YesTrueIt should be23Converted to boolean type.
|OPERATOR:
Pair|The test results are also different from what I imagined,|After the first conversion is returnedFalseValue, if all areFalse,It returns the lastFalse(The value before the type conversion is not performed ).
Example: 
123 | 23Return123Instead of imaginingTrue.
False | nullReturnNull, Instead of imaginingFalse.
Iii. type conversion functions
1.ParseFloatConvert to floating point:
The character parsing function obtains each character until it encounters a character that is not a numerical value.,Then return the value it has obtained..
Several notes:
""(Empty string) ==> NaN
"123e-2" ==> 1.23 (Scientific Computing can be recognized)
"010" => 10 (8The hexadecimal format cannot be recognized.)
"0x10" => 0 (16Hexadecimal unrecognized)
"-010" =>-10
Null, undefined, true, false, new Object (), function () {}=> NaN
2. parseIntConvert to a signed integer:
AndParseFloatSimilar, but he will remove the decimal places (note that it is not rounded down, it is totally discardedMath. floorSame Processing Method),And he can recognize octal and16Hexadecimal Representation:
123e-2 ==> 1
"123e-2" => 123
"010" => 8
"0x10" => 16
"-010" =>-8
"-0x10" ==>-16
Null, undefined, true, false, new Object (), function () {},-Infinity + Infinity NaN ==> NaN
3.Differences between the three integer Functions:
(1)Math. ceil ():"Ceiling"Is it very visual? Is the smallest integer greater than or equal to the parameter.
8.7 => 9
-8.7 ==>-8
(2)Math. floor ():"Floor",Take the smallest integer that is less than or equal to the parameter.
8.7 => 8
-8.7 =>-9
(3)Math. round ():"Rounding"Returns an integer.

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.