Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const todos = [
- { id: 1, text: "Buy groceries", completed: false },
- { id: 2, text: "Finish React project", completed: true },
- { id: 3, text: "Read a book", completed: false },
- { id: 4, text: "Go for a walk", completed: true },
- { id: 5, text: "Call a friend", completed: false },
- { id: 6, text: "Clean the room", completed: false },
- { id: 7, text: "Pay utility bills", completed: true },
- { id: 8, text: "Plan weekend trip", completed: false },
- { id: 9, text: "Water the plants", completed: true },
- { id: 10, text: "Update resume", completed: false }
- ];
- //for loop
- console.log("--USING VANILA JS FOR LOOP--")
- for(let i = 0; i<todos.length; i++){
- console.log(todos[i].text)
- }
- console.log("--USING FOREACH LOOP--")
- //for each loop
- todos.forEach((item)=>console.log(item.text))
- // for in loop - to iterate over objects
- console.log("--USING FOR IN LOOP--")
- for(const idx in todos){
- console.log(todos[idx].text)
- }
- // for of loop
- console.log("--USING FOROF LOOP--")
- for(const todo of todos){
- console.log(todo.text)
- }
Advertisement