Files
30-seconds-of-code/snippets/initializeArrayWithValues.md
Angelos Chalaris 20e7d899f3 Formatting changes
2018-09-27 22:47:46 +03:00

15 lines
387 B
Markdown

### initializeArrayWithValues
Initializes and fills an array with the specified values.
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
You can omit `val` to use a default value of `0`.
```js
const initializeArrayWithValues = (n, val = 0) => Array(n).fill(val);
```
```js
initializeArrayWithValues(5, 2); // [2, 2, 2, 2, 2]
```