Files
30-seconds-of-code/snippets/initializeArrayWithValues.md
Isabelle Viktoria Maciohsek caa67e2a49 Update snippet descriptions
2020-10-20 23:02:01 +03:00

19 lines
477 B
Markdown

---
title: initializeArrayWithValues
tags: array,intermediate
---
Initializes and fills an array with the specified values.
- Use `Array.from()` to create an array of the desired length, `Array.prototype.fill()` to fill it with the desired values.
- Omit the last argument, `val`, to use a default value of `0`.
```js
const initializeArrayWithValues = (n, val = 0) =>
Array.from({ length: n }).fill(val);
```
```js
initializeArrayWithValues(5, 2); // [2, 2, 2, 2, 2]
```