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
  • 7,916
  • 12
  • 56
  • 87
  • 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
  • 572,313
  • 128
  • 898
  • 1,281
  • 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)