This time I will bring you the implicit type conversion in JS, what are the precautions when using the implicit type conversion in JS, the following is a practical case, let’s take a look take a look.
isNaN()
To determine whether it is NaN, Number() will be called first, and then compared with NaN after conversion
isNaN(123); //falseisNaN("123"); //false,字符串//Number('123') ==> isNaN(123) ==> falseisNaN("null"); //true,字符串//Number("null") ==> isNaN(NaN) ==> trueisNaN(null); //false,null对象//Number(null) ==> isNaN(0) ==> falseisNaN(undefined); //true//Number(undefined) ==> isNaN(NaN) ==> trueisNaN("123bcd"); //true//Number("123bcd") ==> isNaN(NaN) ==> true
++/-- +/- (Auto-increment and self-decrement sign, one-yuan positive and negative)
var a = '123'; a ++; //a 124var b = 'abc'; a ++; //b NaN; typeof(b) ==> numbervar c = + 'abc'; //c NaN; typeof(c) ==> number
+ plus sign, when there is a string on both sides of the plus sign, String() will be called to combine both Become a string
var a = 1 + "123"; //"1123"var b = "abc" + 12; //"abc12"
-*% (addition, subtraction, multiplication and division), Number() will be called, and both sides will be converted into numeric types
var a = 1/"2"; //a 0.5;typeof(a) number
&& || ! Convert to boolean value
> < >= <= == 有字符串和数字比较的,隐式的调用Number()转换成数字 //undefined ,null,"abc"都转换成NaNundefined > 0 //falseundefined < 0 //falseundefined == 0 //falsenull > 0 //falsenull < 0 //falsenull == 0 //false"abc" > 0 //false"abc" < 0 //false"abc" == 0 //false
== !=
undefined == undefined //truenull == null //trueNaN == NaN //false
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of h5 game development
html5 animation to realize dancing umbrellas
How to build a server with nodejs
The above is the detailed content of Implicit type conversion in JS. For more information, please follow other related articles on the PHP Chinese website!