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

Source: Internet
Author: User

Recently has been in a 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 of the norms did not understand clearly, Many problems are not caused by specific analysis. 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.

1 true // true converts to a number of 1, so the result is true 2 3 // true 4 5 true // The ' ABC ' is converted to a number Nan, the result is false,ecmascript, and Nan is false compared to any value, including Nan==nan 6
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 varobj = {};2Console.log (obj = = ' 1 ')//obj.valueof () returns this object, and then calls Obj.tostring () to return the ' [Object Object] ' string, converted to a number by comparison of the base type, and found to be unequal, so the result is false3 //to override the ToString method of obj4Obj.tostring () =function(){5     return1;6 }7Console.log (obj = = ' 1 ');//it turns out to be true.8Console.log (obj = =true);//The result is also true
1 varobj = {2ValueOf:function(){3Console.log (' first ');4     },5Tostring:function(){6Console.log (' second ');7     }8 }9Console.log (obj = = 1);//result is first falseTen  One varobj = { AValueOf:function(){ -Console.log (' first '); -         return NULL;//or return undefined the     }, -Tostring:function(){ -Console.log (' second '); -     } + } -Console.log (obj = = 1);//result is first false +  A varobj = { atValueOf:function(){ -Console.log (' first '); -         return{};//or return other compound objects -     }, -Tostring:function(){ -Console.log (' second '); in     } - } toConsole.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//true2 console.log (null/ / False3//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 // 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 // ' A ' is converted to a value of Nan and the result is false 2 // 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 varobj = {};2Console.log (obj < 5);//false3 varobj = {4ValueOf:function() {5Console.log (' first ');6         return2;7     },8Tostring:function() {9Console.log (' second ');Ten     } One } AConsole.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 true // true

Any comparison with Nan will return false

Boolean operator (!) 1. Operands are objects

Any object will return False

1 var obj = {}; 2 // 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 // false 5 // false 6 // true
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//true3//true

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

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.