How to use For…of Loop In Javascript
This For…of Loop in JavaScript works in the same way as the for Loop where we can perform any operation on each element of the given array but it makes it easier as we don’t have to handle the index of the array to traverse the array.
Example: Printing odd number in JavaScript array suing for of loop.
const arr = [1 , 2, 4, 9, 12, 13, 20];
const oddNumbers = []
for(const num of arr){
num % 2 === 1 && oddNumbers.push(num);
}
console.log(oddNumbers);
Output
[ 1, 9, 13 ]
Print Odd Numbers in a JavaScript Array
JavaScript provides us with various approaches for printing odd numbers in an array. The core concept behind finding an odd number is based on the fundamental arithmetic property that odd numbers leave a remainder of 1 when divided by 2.
Table of Content
- Using JavaScript Filter() Method
- Using JavaScript ForEach() Method
- Using JavaScript For Loop
- Using JavaScript For…of Loop
- Using Array.prototype.reduce