How to Multiply Each Member of an Array by a scalar in JavaScript?

Sometimes, we want to multiply each member of an array by a scalar in JavaScript.

In this article, we’ll look at how to multiply each member of an array by a scalar in JavaScript.

Multiply Each Member of an Array by a scalar in JavaScript

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

For instance, we write:

const a = [1, 2, 3];
const b = a.map(x => x * 5);
console.log(b)

to call map to multiply each member of the a array by 5 and assign the returned array to b.

Therefore, b is [5, 10, 15] according to the console log.

Conclusion

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