You can use conditions in loops. Let's look at an example. Let's say we have the following array:
let arr = [1, 2, 3, 4, 5];
Let's print all the elements of this array to the console:
for (let elem of arr) {
console.log(elem);
}
And now we will impose a condition and will display only elements that are even numbers:
for (let elem of arr) {
if (elem % 2 === 0) {
console.log(elem);
}
}
Given the following array:
let arr = [2, 5, 9, 15, 1, 4];
Print to the console those array
elements that are greater than
3
, but less than 10
.
Given the following object:
let obj = {a: 1, b: 2, c: 3, d: 4, e: 5};
Print to the console those object elements whose values are odd numbers.