I noticed something today about Arrays in JavaScript that struck me as a bit odd. I imagine there is an easy explanation that will make perfect sense when I hear it, but until then I thought I would ask the community.
If I use an Array as the sole component of a conditional statement, the statment evaluates to true
whether the array is empty or not. However, if the conditional statement is loosely comparing (==
) the array to a truthy value, then it evaluates to false
, again whether its empty or not. I created a Fiddle to show what I mean: http://jsfiddle.net/dcdruck/X6L8m/11/
Would someone please try to explain to me why an Array is considered truthy on its own but not when compared to another truthy value?