Categories
JavaScript Answers

How to use JavaScript map with a function that has two arguments?

Spread the love

Sometimes, we want to use JavaScript map with a function that has two arguments.

In this article, we’ll look at how to use JavaScript map with a function that has two arguments.

How to use JavaScript map with a function that has two arguments?

To use JavaScript map with a function that has two arguments, we call map with a function that calls the function that takes 2 arguments.

For instance, we write

const values = [1, 2, 3, 4];
const mapped = values.map((n) => square(n, 3));

to call values.map with a function that calls square with n and 3.

n is the value in values being iterated through.

Conclusion

To use JavaScript map with a function that has two arguments, we call map with a function that calls the function that takes 2 arguments.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.