JS循環まとめ


const arr = [1, 2, 3];

// for  :     break, continue, return    ;           
for (let index = 0; index < arr.length; index++) {
    const element = arr[index];
    console.log(element);
}

// forEach:     break, continue    (              ),       return     continue(          )
arr.forEach((value, index) => {
    if (value == 2) {
        return false;
    }
    // console.log(value);
});

// for-in:         ,   break continue, return    
for (const key in arr) {
    if (arr.hasOwnProperty(key)) {
        const element = arr[key];
        // if(element === 2){
            // break;
        // }
        console.log(element);
    }
}

// for-of:             ,       ,  break continue, return    
//        Symbol.iterator         
for (const item of arr) {
    if(item === 2){
        continue;
    }
    // console.log(item);
}

const iterator = arr[Symbol.iterator]();
while(true){
    const next = iterator.next();
    if(next.done){
        break;
    }
    // console.log(next.value);
}

// every:       
arr.every((value, index) => {
    // console.log(index, value);
    return true; //              continue,           break
});