Javascript type conversion methods and precautions

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 type False :
0 , + 0 , -0 , Nan , ""( Null String ), Undefined, null
Other values except the preceding values are converted True Which 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]"
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 ) Constructor Number ();
New number ("123") = 123
Several conversions that require special attention:
"" (Null String) ==> 0
"010" => 10
"0x10" (16 Hexadecimal ) => 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 is String Type, which converts two operands String Type.
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 (Because Undefined Convert to numeric value Nan, All results are NAN)
True + null = 1 (null Convert to number is 0)
"123" + null = "123 null "( Adding a string )
"123" + undefined = "123 undefined" ( Undefined )
1 + 2 + "123" = "3123" (1 + 2 First, 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 is False , Then the expression returns False, If none of the items are False , Then the expression returns the original value of the rightmost item.
Example:
123 & 23 & 45 Return 45, Instead of what we think True.
So if 123 & 23 = true So it should be False .
As If (123 & 23) Yes True It should be 23 Converted to boolean type.
| OPERATOR:
Pair | The test results are also different from what I imagined, | After the first conversion is returned False Value, if all are False, It returns the last False (The value before the type conversion is not performed ).
Example:
123 | 23 Return 123 Instead of imagining True.
False | null Return Null , Instead of imagining False .


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 ( ScientificAlgorithmYes)
"010" => 10 (8 The hexadecimal format cannot be recognized. )
"0x10" => 0 (16 Hexadecimal unrecognized )
"-010" =>-10
Null, undefined, true, false, new object (), function () {}=> Nan
2. parseint Convert to a signed integer:
And Parsefloat Similar, but he will remove the decimal places (note that it is not rounded down, it is totally discarded Math. Floor Same Processing Method ), And he can recognize octal and 16 Hexadecimal 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.

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.