Files
30-seconds-of-code/snippets/initialize-array-with-values.md
Angelos Chalaris 43f8529cb9 Added samples
2017-12-12 17:50:08 +02:00

10 lines
292 B
Markdown

### 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 `v` to use a default value of `0`.
```js
const initializeArray = (n, v = 0) => Array(n).fill(v);
// initializeArray(5, 2) -> [2,2,2,2,2]
```