TIL: A Basic Thing about map()

Lisa R Dean
Jul 8, 2021

So many times I learn or correct my knowledge of things when I’m doing code reviews. TIL that I was under the incorrect assumption that map() would skip over null or undefined array entries. I was wrong. It makes sense in hindsight. Especially since it's in the first line of the documentation. 🤦‍♀️

MDN Docs

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

const original = ["a", null, "b", undefined];
const mapped = original.map((item) => Boolean(item));
I was thinking it would do this: true,true
I was wrong: true,false,true,false

--

--

Lisa R Dean

Imposter Programmer. Former sysadmin. Still on the impossible childhood quest to know everything.