1. Convert other data types to number type
Number()->Strict
parseFloat/parseInt -> Not strict
isNaN(value) If the value value is not of a numeric type, it first calls Number to convert it to a numeric type and then determines whether it is a valid number.
For example:
Number("12px"); ->NaN
parseInt("12px"); ->12
isNaN("12"); ->false
law:
1) If the Boolean type is converted to a number, true->1 false->0
2) Convert null/undefined to a number null->0 undefined->NaN 10+null=10 10+undefined=NaN
3) Convert [] to number. First call the toString method of the array toString method to []->"", and then use the Number method to turn ""->0
2. Convert other data types to boolean types
Boolean
!!
! Convert to Boolean type first, then reverse
For example:
!!1->true
!!0->false
![]->false !![]->true
Rule:
Only 0, NaN, null, undefined, "" are false when converted to Boolean type, and the rest are true when the conversion is completed.
Special circumstances:
If your condition judgment is only a single value, it first converts 1 to Boolean type (->true), and determines whether it is true or false. If it is true, the condition is true, otherwise the condition does not
if (1) { } if ("3px" * 3) {//->The condition does not hold "3px" * 3=NaN ->false } if ("3px" + 3) {//->The conditions are true "3px" + 3="3px3" ->true }
"+" is not just a mathematical operation in JS, but when it encounters a string, it is a string splicing.
"-, *, /" can only be mathematical operations in JS. When encountering a type that is not a number, you must cast to number for mathematical operations.
3. Compare two values (==Compare, ===Compare data types differently, and do not perform default conversion). If the data types on the left and right sides are different, we need to follow a set of rules to perform default data type conversion.
value1==value2
1) Object ==Object. Comparison of the memory addresses stored by itself. If the memory addresses are different, they are not equal. []==[]->false Each empty array is a separate memory address.
2) Object==String Call the toString method to convert the object into a string []==""->true
3) Object == Boolean The object is converted to a number (toString-Number), and the Boolean is also converted to a number (true->1 false->0)
[]==false ->true []->0 false->0 0==0->true
![]==false -> ![] First convert the empty array to a boolean type and then reverse it ->false false==false->true
4) Object == Number Object converted to number []==0->true []==1->false
5) String == Boolean Both are converted to numbers "3"==true->false "1"==true->true
6) String == Number String converted to number ""==0->true
7) Boolean==number Boolean to number true==1->true true==2->false
8)null==undefined ->true null===undefined ->false
null/undefined and any other values are not equal null==0->false
9)NaN==NaN ->false NaN is not equal to any value (including itself)
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!