While I was messing around with truth tables in JavaScript, I noticed that the following evaluates to true:
var a, b, c;
a = {};
b = function(){};
c = a < b;
console.log(c);
Why?
I've only tested this in Firefox, and I'm sure I could dig up the details in the ECMAScript 2.6.2 spec, but TBH I'm feeling lazy.
JavaScript type coercion makes the comparison essentially
String({}) < String(function(){})
so essentially you are just doing
"[object Object]" < "function (){}"
which is a lexicographic string comparison.
Javascript compares objects by calling valueOf()
or toString()
.
Since neither operand has a valueOf()
method, it will compare the toString()
s.
({}).toString()
is [object Object]
.(function() { }).toString()
is function() { }
.
[
is less than f
.
alert(({})) -> [object Object]
alert((function(){})) -> function () {}
[
comes before f
, hence ({}) < (function () {})
.
Yes, it's silly. ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With