Home > Article > Web Front-end > Knowledge explanation of js implicit conversion (with examples)
This article brings you knowledge about js implicit conversion (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
[] == ![] //true ==> "" == false 123 ^ [] //123 ==> 123 ^ 0 ~{} //-1 ==> ~0 {} >= {1,2} //true ==>因为大于等于的比较,不是相等的比较,所以[object Object] >=[object Object] [null] == "" //true ==> [""] == ""
Unary operators: Conversion through Number(); including the * operator , / operator, are all converted by Number()
+undefined //NaN
Logical operator:! Equivalent to Boolean(), converting the operand to a Boolean value Type conversion
Bit operations: ~, |, &, ^;When one operand is NaN, it is equivalent to the operand being 0;
//由以下变化可以证得: NaN ^ NaN ^ NaN = 0
Plus operator, more complex
The highest priority is string, any operand added to a string will be String (x) into a string, and then perform string concatenation
console.log("a" + 1); //"a1" console.log("a" + "1"); //"a1" console.log("a" + false); //"afalse" console.log("a" + undefined); //"aundefined" console.log("a" + NaN); //"aNaN" console.log("a" + null); //"anull" console.log("a" + {}); //"a[object Object]"
The second is number, and the object outputs the string type under normal circumstances
//console.log(1 + "1"); //"11" console.log(1 + 1); //2 console.log(1 + true); //2 console.log(1 + undefined); //NaN console.log(1 + NaN); //NaN console.log(1 + null); //1 console.log(1 + {}); //"1[object,Object]"
When one party is Boolean, or both parties are When it comes to Boolean, it will be processed as Number. The same goes for undefined and null.
console.log(true + true); //2 console.log(true + undefined); //NaN console.log(true + NaN); //NaN console.log(true + null); //1 console.log((true + [NaN])); //"trueNaN"
minus sign, then both sides will be processed by Number()
Comparison operations: ==, >, <, >=, >=, != Follow the rules (excerpted from Elevation 3):
It is worth noting: >= and = between objects =(!=) comparison methods are different. The former is a comparison of the return value of toString(), and the latter is a comparison of the reference address
When both sides are strings, the comparison is based on the character encoding size
When one of the operands is boolean, string, or object, convert it to a number type value and then compare it;
console.log("NaN" == NaN); //false console.log(undefined == null); //true console.log({} >= {1:2}); //true console.log({1:2} != {}); //true console.log({} == {1:2}); //false console.log([1] == [1]); //false console.log(null == 0); //false
The above is the detailed content of Knowledge explanation of js implicit conversion (with examples). For more information, please follow other related articles on the PHP Chinese website!