Don't know why this works


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

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

to me this part...

if (typeof languages[x] === "string")

I understand, its pulling each language.(whatever property) and seeing if its equal to a "string" (I also don't know why the word string is in quotes and not just the word string)

but this line ...

console.log(languages[x]);

is saying to me write out the language.(whatever property name) not the property value.

I dont understand how javascript wrote ou the value and not the property name.

Can someone explain? thanks in advance.


#2

Hello

The typeof operator checks the variable's type and tells to user what type it is...

The "string" is in quotes because the typeof operator makes the type a string and by using === "string " you are asking if the type, in this case, is a string.


#3

This does not really answer the question why

console.log(languages[x]);

is giving the value of the property and not just returning the property name. I just dont get that can you explain further?

Thanks