Why number 4 is printed?


#1

https://www.codecademy.com/courses/objects-ii/5/1?curriculum_id=506324b3a7dffd00020bf661#

Oops, try again. It looks like you printed 4 when you shouldn't have!


 var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var prop in languages){
    if(typeof prop === "string"){
        console.log(languages[prop]);
    }
}


#2

The above is checking the key name, not the associated value, hence all values are logged out.

if (typeof languages[prop] === 'string')

In JavaScript objects, all key names are strings.


#3

Thank you very much!!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.