How to use for loop In Javascript
Looping in programming languages is a feature that facilitates the execution of a set of instructions repeatedly until some condition evaluates and becomes false.
Example: In this example, we are using for loop for creation of an array.
const size = 5;
const array = [];
for (let i = 0; i < size; i++) {
array.push(undefined);
}
console.log(array);
Output
[ undefined, undefined, undefined, undefined, undefined ]
Create an array of given size in JavaScript
Creating an array of a specific size can be achieved through various methods, each suited to different situations. In this article, we will explore several approaches to creating arrays of a desired size, accompanied by examples for each method.
Below are the following ways:
Table of Content
- Using JavaScript array() constructor
- Using apply() and map() Methods
- Using JavaScript Array.from() Method
- Using for loop
- Using the spread syntax with Array.fill()