TIL: A Basic Thing about map()

lisadean

Lisa Dean

Posted on July 8, 2021

TIL: A Basic Thing about map()

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 
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
lisadean
Lisa Dean

Posted on July 8, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related