Looks For-In To Me


#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 x  === "string" ){
 console.log(languages[x]);
}

}

Don't know what is wrong ....


#2

Structure of a for/in loop is:

for __ in _______{}


#3

This is what i wrote didn't I ?


#4

Wait... never mind, you did it correctly (comparing mine to yours :smiley:). Except in your if statement:

You need to show where the x is from, which is languages. Soooo...

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


#5

Yes that's what I found thinking of it twice :bulb:

Thanks anyway :grinning:


#6

what is the difference between

languages.x & languages[x] ?


#7

I don't believe that there's a difference. Both are called the dot notation and the bracket notation, and both call a key/property of the object. So, no, there's no difference.


#8

ı think there is a differance in using at loop


#9

languages.x & languages[x] are the same fucntionally.

However there is a slight advantage for the bracket notation for being able to call a function.

For example, if x is a function, languages[x] would be able to call x as a function whereas languages.x wouldn't be able to.