How do I multiply each member of an array by a scalar in javascript?

Array.map() is available to IE users as of IE9, so if you don’t care about compatibility at all you can use this:

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

For JavaScript 1.8, this is as short as you can go:

var a = [1, 2, 3].map(function(x) x * 5);

If you need maximal browser compatibility, you’ll have to put up with a loop.

Either way you’ll be iterating over the array; Array.map() just makes it less obvious you’re doing so.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)