AyanUpadhaya

For loops in javascript

May 25th, 2025
177
0
Never
6
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const todos = [
  2.   { id: 1, text: "Buy groceries", completed: false },
  3.   { id: 2, text: "Finish React project", completed: true },
  4.   { id: 3, text: "Read a book", completed: false },
  5.   { id: 4, text: "Go for a walk", completed: true },
  6.   { id: 5, text: "Call a friend", completed: false },
  7.   { id: 6, text: "Clean the room", completed: false },
  8.   { id: 7, text: "Pay utility bills", completed: true },
  9.   { id: 8, text: "Plan weekend trip", completed: false },
  10.   { id: 9, text: "Water the plants", completed: true },
  11.   { id: 10, text: "Update resume", completed: false }
  12. ];
  13.  
  14. //for loop
  15. console.log("--USING VANILA JS FOR LOOP--")
  16. for(let i = 0; i<todos.length; i++){
  17.     console.log(todos[i].text)
  18. }
  19.  
  20. console.log("--USING FOREACH LOOP--")
  21. //for each loop
  22. todos.forEach((item)=>console.log(item.text))
  23.  
  24. // for in loop - to iterate over objects
  25. console.log("--USING FOR IN LOOP--")
  26. for(const idx in todos){
  27.     console.log(todos[idx].text)
  28. }
  29. // for of loop
  30. console.log("--USING FOROF LOOP--")
  31.  
  32. for(const todo of todos){
  33.     console.log(todo.text)
  34. }
  35.  
  36.  
  37.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment