for each statement javascript code example
Example 1: for each js
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
fruits.forEach(function(fruit){
console.log('I want to eat a ' + fruit)
});
Example 2: javascript foreach example
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});
Example 3: foreach in javascript
var a = ["a", "b", "c"];
a.forEach(function(entry) {
console.log(entry);
});
var fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
function myFunction(item, index) {
document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
}
Example 4: foreach loop javascript
const numList = [1, 2, 3];
numList.forEach((number) => {
console.log(number);
});
Example 5: foreach javascript
let dogs = ['Husky','Black Lab','Australian Shepard','Golden Retriever'];
dogs.forEach(function(dog) {
console.log(dog);
});
Example 6: foreach js
detail = "<ul>";
result.params.detail.forEach((val) => {
detail = detail + sprintf(lang.pay_credit_detail, val.credit, val.expiry_date);
});
detail += "</ul>";
$('#pay_credit_remain').html(detail);