for in loops javascript code example
Example 1: javascript for loop
for (var i; i < 10; i++) {
}
Example 2: javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item); // Logs each 'Item #'
});
Example 3: javascript loop through array
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
Example 4: for in javascript
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
// expected output:
// "a: 1"
// "b: 2"
// "c: 3"
Example 5: for loop in javascript
For printing 0 to 9
you can write like below also
var i;
for (i = 0; i < 10; i++) {
console.log(i);
}
Example 6: for in loops javascript
for(const key in object){
// do stuff
}
// goes through the keys of an object