Javascript Uniformity

I am trying to understand Javascript equality. Could you tell me why the next line returns false ?

 alert((function a(){}) == (function a(){})) // false 

But, as can be seen from the following cases, < returns false , but <= returns true , which means that == should return true , but this is false . Do you have any ideas WHY?

 alert((function a(){}) < (function a(){})) // false alert((function a(){}) > (function a(){})) // false alert((function a(){}) <= (function a(){})) // true alert((function a(){}) >= (function a(){})) // true 
+4
source share
3 answers

You compare two objects with < , <= , and they are actually compared with a string that they could hide.

"function a(){}" < "function a(){}" is false.

"function a(){}" <= "function a(){}" true.

EDIT: Why (function a(){}) == (function a(){}) returns false because you are comparing the same type with == , so they don’t need to hide a string or number to compare, they are two different objects .

+5
source

A little-known fact is that the Javascript equality operator is actually === not ==. When you run the comparison operator for a function, you are comparing the value of the link (location in memory), not the function itself. Since functions are all separate objects, == will not return true.

From "Javascript - The Ultimate Guide" http://docstore.mik.ua/orelly/webprog/jscript/ch05_04.htm

On the other hand, objects, arrays and functions are compared Help. This means that two variables are equal if they refer to the same object.

+1
source

If we check that the functions are not equal, we get the true value.

 alert((function a(){}) != (function a(){})) //true 

This is because each of the functions is a different object and why the equality check returns false.

0
source

All Articles