How to Fill an Array with False Values in JavaScript?

Estimated read time 1 min read

You can fill an array with false values in JavaScript by using the Array.from() method and passing a constructor function that returns false as its argument.

Here’s an example:

const array = Array.from({length: 5}, () => false);

console.log(array); 
// Output: [false, false, false, false, false]

In this example, {length: 5} creates an array-like object with a length of 5. The arrow function () => false returns false every time it’s called. The Array.from() method creates a new array with 5 elements, each element being the result of calling the arrow function, which returns false.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply