0
[,'a'].every(x => x==='a') 
> True

returns True, although the first item is 'undefined' and therefore should be False?

[,'a'].map(x => x) 
> [undefined × 1, "a"]
DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121
  • 1
    [You should read the docs for `Array.prototype.every()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) – Whymarrh Feb 07 '17 at 23:47

2 Answers2

5

The builtin array methods ignore non-existing properties on sparse arrays. The first item is not undefined, there is no property in index 0 at all. You can try

[,'a'].every(x => x==='a') 
> true
[undefined,'a'].every(x => x==='a') 
> false
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    Proof that is a sparse array: `Object.keys([,'a'])` returns `["1"]`, and not `["0","1"]`. – some Feb 07 '17 at 23:47
-3

Since undefined is a typeof, use == (twice) instead of three times. It returns true because x is empty and therefore the typeof undefined matches undefined (since you use = three times)