Convert to Boolean type
Use two times non-op (!) : Tiantai Yi Zhuang Metallurgy
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 "":
Using string constructors:
1 |
newString(123) = "123". |
Conversions that require special attention:
3 |
-Infinity ==>"-Infinity" |
4 |
+Infinity ==>"+Infinity" |
6 |
undefined ==> "undefined" |
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):
With the constructor number ();
Several conversions that require special attention:
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.
4 |
true+ undefined = NaN (因为undefined转换为数值为NaN,所有结果为NaN) |
5 |
true+ null= 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.
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.
One Yuan non (!) Operator: A non-operator needs to convert the operand to a Boolean type.
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:
2 |
"123e-2"== > 1.23 (科学计算法是可以识别的) |
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:
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.
Math.floor (): "Floor", take the smallest integer less than or equal to the parameter.
Math.Round (): "Rounding" takes an integer.
Summary of JavaScript data type conversion methods