Javascript:undefined!=false and the rules of = = Comparison

Source: Internet
Author: User
Tags scalar

There is a basic concept in JS:
JavaScript in Undefined==null
But Undefined!==null
Undefined and Null are converted to Boolean values are false
If you follow a general idea, such as the following code

var a=1; alert (Boolean (a)); // returns True // then the following code should also return Truealert (1==true); // But for sure, the following code will return Falsealert (1===true);
But for the following code, it is estimated that most JS programmers will wonder
 alert (123==true ); //  why return false  alert (Boolean (undefined));  // outputs false  alert (Boolean (null )); Span style= "color: #008000;" >// outputs false  alert (undefined  ==false ); //  return false  alert (null  ==false ); //  unexpectedly returns false  

For its reasons, the following is a tentative analysis (is a tentative analysis)  

First Look at the js in the = = comparison is how to compare the  
for scalars, the "= =" directly compares the values in the line  
For two objects (this is specifically the object that removes the original value ...). Because in JS can say everything object)  
compares its reference  
Therefore, for the comparison of two objects, see if they are not the same object, it is not necessary to use the = = = (Strictly equal, the value is equal and the same type) of the  


Further analysis of the next JS for scalar application = = Comparison of the situation  
for the same type of two scalar comparison, except NaN is special (Nan==nan return false), there is no doubt  
and for a comparison of two scalars of different types, JS has a set of strict rules, this rule JS parsing engine specifically how to execute, sorry, have not seen the source of JS parsing engine, here had to make a summary of its performance  
1. Boolean,number,string these three types for different types of = = When comparing, the rule is that the values of the two sides are always converted to numbers, and then see if the conversion result numbers are equal  
Code

 alert (1==true ); // true converted to a number is also 1  alert (' 1 ' ==true ); //  Converts the string ' 1 ' and true to a number  alert ( ' ABC ' ==true ); //  Although the string ' abc ' is converted to a Boolean value of true, this will return false  //  because the ' ABC ' is converted to a number that is Nan, and true is converted to a number 1  alert ( 123== ' 123 '); //  same as true  

and undefined and null, are they simple types or reference types?
It is not correct to apply the above rule to undefined and null, before parsing, look at the other forms of = = Comparison

The case of a simple type in JS compared to a reference type = =
This comparison and is regular, and can be used in code to verify the execution of JS internal
When comparing a simple type (which refers to a value other than undefined and null) to an object,
The object's ValueOf method is called first, expecting a scalar to be returned, if the object's ValueOf method returns a compound object.
It then calls the object's ToString method to expect a scalar to be returned, and if it still does not return a scalar, it is sentenced to unequal
If one of the valueof or ToString methods returns a scalar, the scalar is compared with the scalar on the other side of the = =

varobj={};alert (obj= = "abc");//falseobj.tostring=function () {    return' ABC ';}; Alert (obj= = ' abc ');//this will return true.Alert (obj==123);//falseobj.valueof=function() {return123};alert (obj==123);//true!!!obj.valueof=undefined;//clear this method first so that it is not affected by the code aboveAlert (obj==true);//falseobj.valueof=function () {  return1;//as long as it returns a scalar that can be converted to a Boolean value, 1 or True will be OK, which means true};alert (obj==true);//true!!!!

In fact, the following code can be used to verify that when comparing a composite object to a scalar, the object's valueof is always called first.
The ValueOf method returns not a scalar and then calls the ToString method

var obj={    toString:function  () {        alert (' valueof method does not return a scalar, I will be called ');    } ,    valueOf:function  () {        alert (' I was called first, object's ValueOf method default implementation is to return the object itself ');}    }; Alert (1==obj); // you can see the order of execution

So what does this have to do with Undefined!=false?
JS = = To compare the objects on both sides, undefined will be converted into numbers, that is, undefined is considered the basic value
And since undefined is converted to a number Nan, it will always return false when comparing these three types to undefined Number,string,boolean
For NULL,JS, it is compared as an object, that is, attempting to call Null's valueof and ToString method, and then comparing the returned result to another value, you can infer null== False to return false because the valueof implementation of NULL causes the
Because NULL does not have valueof with the ToString method, it always returns false


What does the ECMAScript specification say?
The ECMAScript specification states that A and B are compared if A and B are one of the three types of Number,string,boolean,
And the type A and B are different, then A and B are converted to a number and then compared
Other words

var a= "true"; var b=true; alert (a= =b); // The result is equivalent to the following code alert (number (a) ===number (b));

And if a is one of the three types of Number,string,boolean, and B is a composite object (Object,array, etc.)
Performs a toprimitive operation on B (This step is performed by the JS interpreter)
That

 var  a= "abc"  var  b={};alert (a  ==B);  //  in JS parsing. // a==toprimitive (b);  

The implementation of this toprimitive method is to call the Valueof,tostring method of the object in turn until one of the methods returns a base value
If these two methods do not return a base value, they are not considered equal

And when both A and B are compound objects, it's easy to see if A and B are references to the same object!

For the comparison of Undefined,null with other types, it is not specifically said that it will
When any base type is compared to a Boolean type, it is converted to a number (as in the previous rule)


Summary
Number,boolean,string,undefined these basic types are mixed and compared, they are converted to numbers and then compared
When a base type is compared to a composite object, the composite object is first converted to the base type (called ValueOf and the ToString method in turn) for comparison
Undefined is treated as a base type, undefined is converted to a number that is Nan, so undefined always returns false when compared to a value other than null (note Nan==nan returns false)
Null is treated as a compound object, because null has no valueof with the ToString method, so it always returns false when compared to a value other than undefined

Javascript:undefined!=false and the rules of = = Comparison

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.