Files
30-seconds-of-code/snippets/initialize-array-with-values.md
Angelos Chalaris e2d2b6aa41 Updated snippets
Mainly for better readability
2017-12-12 18:21:53 +02:00

304 B

Initialize array with values

Use Array(n) to create an array of the desired length, fill(v) to fill it with the desired values. You can omit value to use a default value of 0.

const initializeArray = (n, value = 0) => Array(n).fill(value);
// initializeArray(5, 2) -> [2,2,2,2,2]