Why if i use var x = languages.key; in place of var x = languages[key]..my code not run;


#1


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

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


#2

because of your for loop, key will holds the key, but as string

you can only use the dot notation if it the object property (not in string form)


#3

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