Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

types of loops in javascript

//a for-of loop async-friendly
for (const element of theArray) {
    // ...use `element`...
}

//forEach  not async-friendly
theArray.forEach(element => {
    // ...use `element`...
});

//old-fashioned for loop
for (let index = 0; index < theArray.length; ++index) {
    const element = theArray[index];
    // ...use `element`...
}

//for-in
for (const propertyName in theArray) {
    if (/*...is an array element property (see below)...*/) {
        const element = theArray[propertyName];
        // ...use `element`...
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #types #loops #javascript
ADD COMMENT
Topic
Name
5+1 =