Loop Through A JavaScript Object By Keys

November 2022

The for...in syntax looks like the most susinct way to go:

JavaScript
const example1 = {
    a: "alfa",
    b: "bravo",
    c: "charlie",
    d: "delta"
}

for (const k in example1) {
    console.log(k)
}
Output:
a
b
c
d

There is also this of approach if needed (not sure what the difference is. leaving it here for now)

JavaScript
const example2 = {
    a: "alfa",
    b: "bravo",
    c: "charlie",
    d: "delta"
}

for (const k of Object.keys(example2)) {
    console.log(k)
}
Output:
a
b
c
d
end of line