简体   繁体   中英

why I got error when "push" the element to array list in javascript?

I wrote the code, but I don't know where the mistake is?

I want to fill out the numberList array from 1 to 6 by for/loop and push.

let numberList = [];


for (let i = 0; i <= 6; i++) {
  numberList[i].push();
}
console.log(numberList);

Your current syntax is wrong . The actual Array.prototyp.push() syntax is like below,

push(element0)
push(element0, element1)
push(element0, element1, ... , elementN)

Code: : If want to fill out the numberList array from 1 to 6 by for/loop and push, then start i from 1 not 0

 let numberList = []; for (let i = 1; i <= 6; i++) { numberList.push(i); } console.log(numberList);

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