Filling an array in JavaScript
You can fill arrays with data in a loop.
To do this, use the push
method:
let arr = [];
for (let i = 1; i <= 5; i++) {
arr.push(i);
}
console.log(arr); // shows [1, 2, 3, 4, 5]
Using a loop and the push
method,
fill an array with numbers from
1
to 10
.
Using a loop and the push
method,
fill an array with 10
letters 'x'
.
Given an array of numbers. Loop through it and write only positive numbers into the new array.