본문 바로가기

Programming/JavaScript

Looping Arrays, Breaking and Continuing

const sunnyArray = [
    'Sunny',
    'Ryu',
    2022 - 1988,
    'technical writer',
    ['Writing', 'Coding', 'Learning']
];

for (let i = 0; i < sunnyArray.length; i++) {
    console.log(sunnyArray[i]);
}

console.log(sunnyArray[i], typeof sunnyArray[i]);

const types = [];

for (let i = 0; i < sunnyArray.length; i++) {
// Reading from sunny array
    console.log(sunnyArray[i], typeof sunnyArray[i]);

// Filling types array
//  types.push(typeof sunnyArray[i]);
    types[i] = typeof sunnyArray[i];
}

console.log(types);
// prints (5) ['string', 'string', 'number', 'string', 'object']
const years = [1991, 1992, 1993, 1994];
const ages = [];

for (let i = 0; i < years.length; i++) {
    ages.push(2037 - years[i]);
}
console.log(ages);

Continue vs. Break

The continue statement (with or without a label reference) can only be used to skip one loop iteration. The break statement, without a label reference, can only be used to jump out of a loop or a switch.

// continue and break
console.log('---ONLY STRINGS')
for (let i = 0; i < sunnyArray.length; i++) {
    if (typeof sunnyArray[i] !== 'string') continue;
    console.log(sunnyArray[i], typeof sunnyArray[i]);
}

https://www.w3schools.com/js/js_break.asp

 

JavaScript Break and Continue

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

'Programming > JavaScript' 카테고리의 다른 글

Looping Backwards and Loops in Loops  (0) 2022.09.19
The while Loop  (0) 2022.09.19
Iteration: The for Loop  (0) 2022.09.15
Coding Challenge #3 - comparing BMIs using objects  (0) 2022.09.15
Object Methods  (0) 2022.09.15