DEV Community

Thomas McDonald
Thomas McDonald

Posted on

.map() and console.log: A Love Story

Have you ever found yourself in a situation where you have reluctantly rewritten that nice one-line map function just to log your outputs?

const data = ['1', '2', '3'];

const mappedData = data.map((el) => {
    console.log(el);

    return Number(el);
});
Enter fullscreen mode Exit fullscreen mode

Well, now you don't have to. You see, console.log returns undefined which is perfect for a conditional statement.

const data = ['1', '2', '3'];

const mappedData = data.map((el) => console.log(el) || Number(el));
Enter fullscreen mode Exit fullscreen mode

Thank you for participating in my Tom Blog.

Top comments (0)