0

I was testing the filter function for an array of objects, but by mistake I defined b as an array of numbers, so I decided to see if the filter function gives me an error if I accessed an invalid property, but unexpectedly it works, and i don't know why, could someone explain to me why it works?

a = {a:5};
b = [1,2,3,4,5];
console.log(b.filter(x => x.z === a.a.z).pop()); // 5
Hector Marin
  • 118
  • 5

1 Answers1

2

a.a is 5. a.a.z is undefined.

x is always a number. x.z is undefined.

undefined === undefined

The filter copies all the values in the array.

Pop pulls the last value off the array. It is 5.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335