The method of JavaScript type conversion and the problems needing attention (quite comprehensive) _javascript tips

Source: Internet
Author: User
Tags numeric logical operators object object
First, the type conversion method and should pay attention to the question:
1. Convert to Boolean:
(1) with two-time non-operation(!):
!! 5 ==> True
(2) with a Boolean constructor:
New Boolean (5) = = > True
Value is converted to a Boolean type ofFalse:
0,+0,-0,NaN,""(Empty string), Undefined,null
In addition to the value of the above other values after the conversion forTrue, it is necessary to mention in particular:
"0", New Object (), function () {}
2. Convert to String type:
(1) plus an empty string"":
123 +   "" = "123"
(2) with a string constructor:
New String (123) = "123".
Transformations requiring 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) Take positive (+), Minus 0 (-0), by one, (*1), divided by one (/1),Take a negative (-, this gets the opposite value).
+ "123" = 123
+true = 1
(2) with the constructorNumber ();
New Number ("123") = 123
Several transformations which require special attention:
""(empty string)==> 0
"010" ==> 10
"0X10" (16Into the system) ==> 16
" -010" ==>-10
" -0x10" ==> NaN
Undefined ==> NaN
Null ==> 0
True ==> 1
False ==> 0
New Object () ==> NaN
New function () {} ==> NaN

Two, implicit type conversion:
(1)binary addition operation (+): If one of the two operands isStringType, converts the two operands to theStringType is added again.
If there are no string types in the two operands, then the two operands are converted to numeric types and then the operation is done.
Example:
"123" +123 = "123123";
123+123 = 246
True + true = 2
true + undefined = NaN(BecauseUndefinedConvert to a value ofNaN,All results areNaN)
true + NULL = 1 (nullConverting to numbers is0)
"123" + null = "123null" (The containing string is converted to a string addition)
"123" + undefined = "123undefined"(UndefinedEqually applicable)
1 + 2 + "123" = "3123" (1+2is calculated first according to the numerical model)
(2)Binary subtraction and multiplication operation (-*/):Because only numeric types have- * /Operation, the first two operands are converted to numeric types and then the operation is done.
"123"-123 = 0
(3)One-dollar take positive (+), take the negative operator(-):A positive or negative is an operation on a numeric type, so the operand is converted to a numeric type and then an operation is taken.
+ "123" = 123
-"123" =-123
+ "123e" = NaN
+ "123f" = NaN
+ "123e-2" = 1.23
(4)One dollar is not(!)Operator:Non-operators need to convert operands to Boolean types.
!" 123 "= False
!!" 123 "= True
!123 = False
!! 123 = True
(5)Logical operators (&&and||):
In&&Or||The sides are judged by the Boolean type,
But when I was testing, I found an interesting thing.
&&Operator: If there is an item that isFalse, an expression returnsFalseIf all of the items are notFalse, an expression returns the original value of the rightmost item.
Example:
123 && && 45Return45,And not what we imagined.True.
So if there's123 && = = TrueThen it should be.False。
As forif (123 && 23)Considered to beTrueThat's supposed to be23Converted to a Boolean type.
||Operator:
Right||The results of the test are not the same as I imagined,||Returns the first conversion after notFalseValue, if all of theFalseIt will return the last one toFalseValue (a value that was not preceded by a type conversion).
Example:
123 | | 23Return123, not the imaginaryTrue.
False | | NullReturnNull, Rather than imagined.False。
three, type conversion function
1. parsefloat convert to floating-point number:
Character resolution function gets every character until it encounters a character that is not a numeric value,And then returns the value that it has obtained.
A few need special attention:
""(Empty string) ==> NaN
"123e-2" = > 1.23 (The scientific method of calculation can be identified)
"010" ==> 10 (8The system does not recognize)
"0x10" ==> 0 (16The system does not recognize)
" -010" ==>-10
Null,undefined,true,false,new Object (), function () {} ==> NaN
2.parseInt convert to signed integer :
WithParsefloatSimilar, but he would give up the decimal place (note not rounded, is completely discarded, withMath.floorHandled the same way),And he can recognize the octal system and16The way in which the system is represented:
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. The difference between three rounding functions :
(1 ) math.ceil (): " ceiling " , very image? is to take the smallest integer that is greater than or equal to the argument.
8.7 = = > 9
-8.7==>-8
(2 ) math.floor ():" floor takes the smallest integer less than or equal to the argument.
8.7 ==> 8
-8.7 ==>-9
(3 ) math.round ():" rounded takes 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.