for (key in object) {
console.log(key,object[key]);
}
//basic for in loop
const car = {
carName: "Ford",
carDoor: 4,
carSpeed: 180
}
for (objKey in car) {
console.log(car[objKey])
}
//output
Ford
4
180
let list = ["a", "b", "c"];
// for in
for (let i in list) {
// i is index
console.log(i); // "0", "1", "2",
console.log(list[i]); // "a", "b", "c"
}
// for of
for (let i of list) {
// i is value
console.log(i); // "a", "b", "c"
}
const obj = {
a: 1,
b: 2,
c: 3,
};
// Long-hand
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = obj[key];
// ...
}
// Short-hand
for (const key in obj) {
const value = obj[key];
// ...
}