this had me surprised:
var parts = email.split('@');
if (parts < 2) {
reference: https://github.com/Kicksend/mailcheck/blob/master/src/jquery.mailcheck.js#L21-22
essentially, it looks like:
var a = [null, null, null]
a < 2 // false
var b = [null]
b < 2 // true
so it seems like it works but I want to know why, what coercion actually happens for it to do this? as the intent here was to bail if less than 2 parts come from the email string, i would have expected it to always pass due to the array defined being truthy – it ought to create an array with at least 1 member even on an empty string.
i would always prefer to use array.length. is the above safe?
It’s a string and then number conversion that’s happening, since arrays can’t be directy compared with the
<operator. First it will do a string comparison but with the number it will then do a number comparsion.Note that:
[null, null, null] == ",,"[null] == ""And:
+",,"isNaN+"" === 0Now it makes sense because
0 < 2 === truebutNaN < 2 === false.It’s therefore not a meaningful expression indeed.
[null, null] < 3isfalsebecause"," < 3is essentially doingNaN < 3.