New article, new snippets, new collection

This commit is contained in:
Angelos Chalaris
2023-05-14 12:21:52 +03:00
parent 26a6b8f874
commit b4e1bdd7f8
8 changed files with 204 additions and 4 deletions

View File

@ -3,18 +3,19 @@ title: Initialize array with values
type: snippet
language: javascript
tags: [array]
author: chalarangelo
cover: flower-portrait-1
dateModified: 2020-10-20T23:02:01+03:00
---
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.
- Use the `Array()` constructor to create an array of the desired length.
- Use `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);
const initializeArrayWithValues = (n, val = 0) => Array(n).fill(val);
```
```js