'Why for in loop returns string instead of my object? [duplicate]

i have nested object:

var obj = {
    nestobj1:{
        title: "some 1 title",
        text: "some text"
    },
    nestobj2:{
        title: "some 2 title",
        text: "some text"
    }
}

I am using for in loop

for ( let s in obj) {
    console.log(s);
}

Console logs strings: nestobj1 and nestobj2. Why? Why it dont returns/logs object ? Why it is string ? Please forgive me i quite new in Javascript.



Solution 1:[1]

The for...in statement iterates over all enumerable properties of an object.

The way you do it you only get the property name of the object without its value. If you want to get the nested objects (the values) then you need to do it like that:

for ( let s in obj) {
    console.log(obj[s]);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stundji