Languages
[Edit]
EN

JavaScript - fill array with default values (ES6+)

0 points
Created by:
Kadeem-Craig
456

In this article, we would like to show you how to fill array with default values in JavaScript (ES6+).

Quick solution:

// ONLINE-RUNNER:browser;

let array = Array(3).fill(0);
console.log(array); // 0,0,0

 

Practical example

In this example, we create an array with size 5 and use fill() method to fill all its elements with 1 as default value.

// ONLINE-RUNNER:browser;

let array = Array(5).fill(1);
console.log(array); // 1,1,1,1,1

References

JavaScript - Arrays (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join