Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access array outside for loop

I want to access array variable outside the loop. but its returning null. below is sample code.

var result = [];
for (var i=0; i < 10; i++) {
     result.push[i];
}
like image 357
Jay Avatar asked Sep 11 '26 19:09

Jay


1 Answers

The syntex of push method is push() not push[].

var result = [];
for (var i=0; i < 10; i++) {
    result.push(i);
}
console.log(result);

For more info about push() look How to append something to an array?

like image 93
Sudhir Ojha Avatar answered Sep 14 '26 10:09

Sudhir Ojha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!