Why does ",,," == Array(4) in Javascript? -
boot interpreter/console , try comparison
> ",,," == array(4) true
why? @ first thought maybe since think of ",,," array of 4 characters '\0' terminating slice, might why, but
> "..." == array(4)
returns "false". so... why? know it's idiosyncratic bit of duck typing in javascript, curious underlines behavior. gleaned zed shaw's excellent presentation here btw.
because right hand operand converted string , string representation of array(4)
,,,
:
> array(4).tostring() ",,,"
if use array constructor function , pass number, sets length of array number. can have 4 empty indexes (same [,,,]
) , default string representation of arrays comma-separated list of elements:
> ['a','b','c'].tostring() "a,b,c"
how comparison works described in section 11.9.3 of specification. there see (x == y
):
8. if type(x) either string or number , type(y) object,
return result of comparison x == toprimitive(y).
(arrays objects in javascript)
and if follow toprimitive
method find it calls tostring
.
Comments
Post a Comment