Skip to content

Instantly share code, notes, and snippets.

@cyberbarbie
Created September 4, 2021 02:11
Show Gist options
  • Save cyberbarbie/6b4382c9907b5abac1bcd601340a7728 to your computer and use it in GitHub Desktop.
Save cyberbarbie/6b4382c9907b5abac1bcd601340a7728 to your computer and use it in GitHub Desktop.
JavaScript forEach() to iterate list & output value and index updated by 1
let todos = ["Sleep", "Eat", "Pray", "Love"]
// Use forEach() to loop through array and print current index:value
todos.forEach((value, index) => console.log(`${index+=1}: ${value}`))
// output:
/*
1: Sleep
2: Eat
3: Pray
4: Love
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment