javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item); // Logs each 'Item #'
});
3.5
2
<!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>
Thank you!
2
0
0
10
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
*/
Thank you!
10
0
3.4
5
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
Thank you!
5
0
4.3
10
var colors=["red","blue","green"];
for(let color of colors){
console.log(color)
}
Thank you!
10
0
0
0
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]);
}
Thank you!
0
0
0
0
@bot.inline_handler(lambda query: query.query == 'text')
def query_text(inline_query):
try:
r = types.InlineQueryResultArticle('1', 'Result', types.InputTextMessageContent('Result message.'))
r2 = types.InlineQueryResultArticle('2', 'Result2', types.InputTextMessageContent('Result message2.'))
bot.answer_inline_query(inline_query.id, [r, r2])
except Exception as e:
print(e)
Thank you!
0
0
5
1
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
});
Thank you!
1
0
4.25
8
var items=["a","b","c"];
for(let item of items){
console.log(item)
}
Thank you!
8
0
4.13
8
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>";
}
Thank you!
8
0
Are there any code examples left?
New code examples in category Other