简体   繁体   中英

how to use for-loop in array (javascript)

var arr = [23,56,87,109];
var a23 = arr[0];
var a56 = arr[1];
var a87 = arr[2];
var a109 = arr[3];

How can I do that by using "for-loop"? THanks!

Try the code below.

var arr = [23,56,87,109];
for (var i = 0; i < arr.length; i++) {
    alert(arr[i]);
    //Do something
}

I am not sure why do you want to assign it to N - separate variables.But you can use for loop like this :

 var arr = [23, 56, 87, 109]; for (var i = 0; i < arr.length; i++) { document.write(arr[i] + "<br>"); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM