javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item); // Logs each 'Item #'
});
javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item); // Logs each 'Item #'
});
javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
// Here's 4 different ways
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
for (let index in array) {
console.log(array[index]);
}
for (let value of array) {
console.log(value); // Will each value in array
}
array.forEach((value, index) => {
console.log(index); // Will log each index
console.log(value); // Will log each value
});
javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
for (let index = 0; index < array.length; index++) {
console.log("index", index);
console.log("array item", array[index]);
}
javascript loop
JavaScript supports different kinds of loops:
for - loops through a block of code a number of times
for/in - loops through the properties of an object
for/of - loops through the values of an iterable object
while - loops through a block of code while a specified condition is true
do/while - also loops through a block of code while a specified condition is true
for (let i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
javascript loop
//There are many loops in javascript that get the job done
let colors = ["red","blue","green"];
//More detail about loop => shorturl.at/avIRW
for(let i in colors){
console.log(colors[i])
}
//More detail about loop => shorturl.at/hBUW2
colors.forEach(color => {
console.log(color)
})
//More detail about loop => shorturl.at/pLOV6
colors.map(color => {
console.log(color)
})
//More detail about loop => shorturl.at/hlstV
for(let i = 0; i < colors.length; i++){
console.log(colors[i])
}
//More detail about loop => shorturl.at/iKS38
let i = 0;
while ( i < colors.length ){
console.log(colors[i])
i++
}
javascript loop
var items=["a","b","c"];
for(let item of items){
console.log(item)
}
javascript loop
var colors=["red","blue","green"];
for(let color of colors){
console.log(color)
}
javascript loop
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
JavaScript Loop
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic for loop example improved</title>
<style>
</style>
</head>
<body>
<p></p>
<script>
const cats = ['Bill', 'Jeff', 'Pete', 'Biggles', 'Jasmin'];
let info = 'My cats are called ';
const para = document.querySelector('p');
for(let i = 0; i < cats.length; i++) {
if(i === cats.length - 1) {
info += 'and ' + cats[i] + '.';
} else {
info += cats[i] + ', ';
}
}
para.textContent = info;
</script>
</body>
</html>
javascript loop
let array = ["foo", "bar"]
let low = 0; // the index to start at
let high = array.length; // can also be a number
/* high can be a direct access too
the first part will be executed when the loop starts
for the first time
the second part ("i < high") is the condition
for it to loop over again.
the third part will be executen every time the code
block in the loop is closed.
*/
for(let i = low; i < high; i++) {
// the variable i is the index, which is
// the amount of times the loop has looped already
console.log(i);
console.log(array[i]);
} // i will be incremented when this is hit.
// output:
/*
0
foo
1
bar
*/
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us