27. Looks For-In to me


I cannot figure out why my output is coming back as undefined I am sure it is a syntax error but I cant see where

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

// print hello in the 3 different languages
for (var key in languages) {
  if (typeof languages[key] === "string"){

Any help would be appreciated


console.log(languages.key) ==> console.log(languages[key])


So the issue is your last line. I was actually running into this problem also, and what I gathered is that you need to use bracket notation rather than dot notation. I believe by saying:


The console thinks you want to print the value of the property name "key". The languages object only has property names of english, french, notALanguage, and spanish. By using languages.key, you're pointing to an object that does not actually exist.

Now the reason that bracket notation will work is because in the for-in statement, you are assigning key to be equal to one of the variables in languages. if you do

console.log(typeof key)

in your for-in loop, you will see that the four property names in languages are of type string. So you are assigning a string to key, then you put key inside of the brackets. A string (key) in brackets is the valid syntax for bracket syntax. Hopefully that makes sense to you.


Thanks that was really helpful