“Para o loop abreviado JavaScript” Respostas de código

Para o loop abreviado 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

para i

# For loops in Dip

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

Para Loop Shatand em JS

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

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

Para Loop Shatand em JS

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

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

Respostas semelhantes a “Para o loop abreviado JavaScript”

Perguntas semelhantes a “Para o loop abreviado JavaScript”

Mais respostas relacionadas para “Para o loop abreviado JavaScript” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código