“Für Loop Shorthand JavaScript” Code-Antworten

Für Loop Shorthand JavaScript

// for loop shorthand javascript
// Longhand:
const animals = ['Lion', 'Tiger', 'Giraffe'];
for (let i = 0; i < animals.length; i++){
  console.log(animals[i]);
}

// Shorthand:
for (let animal of animals){
    console.log(animal);
}

// If you just wanted to access the index, do:
for (let index in animals){
  console.log(index);
}

// This also works if you want to access keys in a literal object:
const obj = {continent: 'Africa', country: 'Kenya', city: 'Nairobi'}
for (let key in obj)
  console.log(key) // output: continent, country, city
  
// Shorthand for Array.forEach:
function logArrayElements(element, index, array) {
  console.log(index + " = " + element);
}
["first", "second", "third"].forEach(logArrayElements);
// 0 = first
// 1 = second
// 2 = third
Chetan Nada

für i

# For loops in Dip

for i = 0 to 5 then print(i)
Splendid Shark

für Loop Shothand in JS

const fruits = ["Apple", "Mango", "Peach"];

for (let fruit of fruits) console.log(fruit);
Ill Ibis

für Loop Shothand in JS

const fruits = ["Apple", "Mango", "Peach"];

for (let i = 0; i < fruits.length; i++) {
  const fruit = fruits[i];
  console.log(fruit);
}
Ill Ibis

Ähnliche Antworten wie “Für Loop Shorthand JavaScript”

Fragen ähnlich wie “Für Loop Shorthand JavaScript”

Weitere verwandte Antworten zu “Für Loop Shorthand JavaScript” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen