Comparison in Javascript
I read some article about basic javascript recently, and having more understading of the concept of 『comparison』
In javascript, the symbol 『 = 』 have two usages
First one is to given a value of a variable, such as: let x = 1
Second one is compaison, and there are two compare in javascript, strict equality (a.k.a. ===), loose equality (a.k.a. ==) the difference between these two is
in strict equality(===) compare only when two type are the same, if operands are same type and having same value or reference the same object. it'll return true or return false
However; in loose equality (==) javascript do the coercion behind you the coercion will transform two different type to the same one then do the compare therefore, sometimes weird things happened. for example as following:
[0] == ""; //false
[0] == 0; //true
[""] == ""; //true
[""] == 0; //true;
How come [0] == '' is false, but others are true when you know what javascript doing, it will be no more confusing
So, what javascript doing behind base on some rules According to this documents from MDN Roughly speaking, javascript try to change two type the same by rules:
- When same type in comparison
Same type compare reference here
- When Two type in comparison
-
Object vs Primitive
=> try turning Object into Primitive => by doing these methods in order : valueOf() or toString() => if valueOf() doesn't become Primitive, then use toString() -
Primitive(Boolean) vs Prmitive(rest Primitive like: String, Number)
=> trun Boolean into Number, refernce here -
Primitive(String) vs Primitive(Number)
=> turn String into Number reference here
as earlier example
//1.
[0] == ""; //object vs string
// because [0].valueOf() still[0] so changing method : toString()
"[0]" == "";
// then compare two String type, equal to false
//2.
[0] == 0; // object cs number
// as above 👆
"[0]" == 0;
// then do String to Number : "[0]" -> NaN,
NaN == 0; //false
// rest of questions can using the same logic to figure it out
when I read again of an article of iThome blog I find that I have a little bit more understanding about javascript or, I think I just didn't know javascript well before hope that I can do better, making some progress
update
I found something I miss before for example:
// (1)
"" == false; // true!
// (2)
null == undefined; // true!
// => in mdn Equality (==) topic, description part2
// "If one of the operands is null or undefined, the other must also be null or undefined to return true. Otherwise return false."
// so the two comparison will be true
// (3)
// In ">","<",">=","<=" situations
// null will transform to number, so it will be 0
// undefined will turn into NaN
// so in above situations, always false
// (4) so the following examples
// null vs 0 !?
null > 0;
null < 0;
null == 0;
null >= 0;
// the answer are...
// false!
// false!
// false!
// true!!!!
/*
because in ">" "<" comparison,it will compare in number type(or based on the values of the Unicode code points they contain of both string)
so in first two examples, null will turns into 0 (undefined will turns into NaN)
therefore, 0 > 0, 0 < 0 both false
and 0 >= 0 be true
and ==,like I mentioned above
null return true when the other is alos null or undefined, otherwise, false
/*
this is today's sharing 👋