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];
}
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];
}
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?
push is a method implemented on array. The basic syntax of invoking or calling a function is to specify parenthesis ()
after the function name.
The push() method adds one or more elements to the end of an array and returns the new length of the array.
var result = [];
for (var i=0; i < 10; i++) {
result.push(i);
}
console.log(result);
Please use the below code:
var result = [];
for (var i=0; i < 10; i++) {
result.push(i);
}
You can do that like this also.
var result = [];
for (var i=0; i < 10; i++) {
result[i]=i;
}
If you want to use push
then use like this result.push(i)