add values to array using for loop in javascript code example
Example 1: putting a loop into an array javascript
function test() {
var sub_array = [];
var super_array = [];
for (var i = 1; i <= 3; i++) {
sub_array.push(i);
super_array.push(sub_array.slice(0));
}
alert(super_array);
}
Example 2: how to insert data in array via for loop js
function add(new_element){
let arr = [1,2,3,4,5];
for(let i=0;i<arr.length;i++)
{
arr.push(new_element);
}
return arr;
}
console.log(add("a"));