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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *