Home > Web Front-end > JS Tutorial > Knowledge explanation of js implicit conversion (with examples)

Knowledge explanation of js implicit conversion (with examples)

不言
Release: 2018-09-20 17:04:37
Original
2117 people have browsed it

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.

Appetizers

[] == ![]                  //true  ==> "" == false
123 ^ []                   //123   ==> 123 ^ 0
~{}                        //-1    ==> ~0
{} >= {1,2}                //true  ==>因为大于等于的比较,不是相等的比较,所以[object Object] >=[object Object]
[null] == ""               //true  ==> [""] == ""
Copy after login

Notable operators:

  • Unary operators: Conversion through Number(); including the * operator , / operator, are all converted by Number()

+undefined   //NaN
Copy after login
  • 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
Copy after login
  • 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]"
Copy after login

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]"
Copy after login

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"
Copy after login
  • minus sign, then both sides will be processed by Number()

  • Comparison operations: ==, >, <, >=, >=, != Follow the rules (excerpted from Elevation 3):

1.null and undefined are equal
2. Before comparing for equality, null and undfined cannot be converted into any other value
3. If one of the operands is NaN, the equality operator returns fasle, the inequality operator returns true, and NaN is not equal to NaN
4. Comparison between two objects, both point to the same object (same address), the equality operator returns true, otherwise it returns false

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template