Categories
JavaScript Answers

How to multiply each member of an array by a scalar in JavaScript?

Spread the love

To multiply each member of an array by a scalar in JavaScript, we use the map method.

For instance, we write

const a = [1, 2, 3].map((x) => {
  return x * 5;
});

to call map with a callback to return x in the [1, 2, 3] array being looped through by 5.

An array with each value in [1, 2, 3] multiplied by 5 is returned.

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 *