JS medium sex operator (= =), relational operator (<,>), and Boolean operator (!) Comparison rules

Source: Internet
Author: User

Recently has been in the written interview, often encountered such as 123== ' 123 ', ' abc ' ==true and other issues, including the correct answer, there is also wrong, the main reason or the ECMAScript norms did not understand clearly, many of the problems are not specific analysis caused. A summary of the information is available after the review.

Equality operator (= =) 1, the type of comparison is the basic types of string, number, Boolean

When comparisons are made between these types, the two sides are converted to numbers before they are compared.

2. When comparing the above basic types with reference types

The valueof () method of the object is called first, expecting to return a value of the base type ( tested to return a Boolean, number, String, null, undefined, or no return value, the ToString method will not continue to be called) , if the return is still a compound object, then call its ToString () method, if still do not return the above basic type, it is directly sentenced to unequal.

1 var obj = {};2 Console.log (obj = = ' 1 ')//obj.valueof () returns this object, so then call Obj.tostring () to return the ' [Object Object] ' string, by comparison of the base type, are converted to numbers and found to be unequal, so the result is the ToString method of FALSE3//rewrite obj 4 obj.tostring () = function () {5     return 1;6}7 console.log (obj = = ' 1 '); The result is true for 8 console.log (obj = = true); The result is also true
1 var obj = {2     valueof:function () {3         console.log (' first '); 4     }, 5     tostring:function () {6         console. Log (' second '); 7     } 8} 9 console.log (obj = = 1); The result is first False10 one-by-one var obj = {     valueof:function () {         console.log (' first ');         return null;//or return U Ndefined15     },16     tostring:function () {         console.log (' second ');}19}20     console.log (obj = = 1); /result is first false21, var obj = {     valueof:function () {         console.log (' first '), +/-         return {}; His compound object is     },27     tostring:function () {         console.log (' second ');     }30}31 console.log (obj = = 1); The result is first second false

For undefined and null,ecmascript, the null==undefined result is true; you cannot convert null and undefined to other values until you compare them.

1 Console.log (null = = undefined); True2 Console.log (Null = = 0); False3 Console.log (0 = = undefined); False
3, two objects comparison

Returns true if two operands point to the same object, otherwise false.

Relational operator (<,>) 1, both sides are strings

is a string, the character encoding that corresponds to the string is compared

1 console.log (' abc ' < ' ADC ')//true
2. One operand is a numeric value

If one operand is a numeric value, the other is converted to a numeric value for comparison

1 Console.log (' a ' > 5); ' A ' is converted to a value of Nan, resulting in false2 console.log (' 2.3 ' > 1); True
3, there is an operand is an object

The valueof method of the object is called, and the returned result is compared with the previous rule, and if there is no valueof method, the ToString method is called

1 var obj = {}; 2 Console.log (obj < 5); False 3 var obj = {4     valueof:function () {5         console.log (' first '); 6         return 2; 7     }, 8     Tostring:fun Ction () {9         console.log (' second '),     }11}12 console.log (obj < 5);//first true
4. There is one operand that is a Boolean value

Convert this Boolean value to a numeric value before you compare it

1 Console.log (' 3 ' > True); True

Any comparison with Nan will return false

Boolean operator (!) 1. Operands are objects

Any object will return False

1 var obj = {};2 Console.log (!obj); False
2, operand is a string

Null string returns True, non-null returns false

1 var a = ' OK '; 2 var b = ';  3 var c = '; 4 console.log (!a);//false5 Console.log (!b);//false6 Console.log (!c);//tru E
3, the operand is the value

0 returns True, not 0 (including infinity) returns false

4, the operation number is Null,undefined,nan

Returns True

1 Console.log (!null); True2 Console.log (! NaN); True3 Console.log (!undefined); True

JS medium sex operator (= =), relational operator (<,>), and Boolean operator (!) Comparison rules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.