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 property in languages){
if(typeof languages.property===“string”)
console.log(languages.property)}

why it doesnot work?
if i changed it into [];
it works .but i remember that [] and . are the same result .why it is different?
thanks

for(var property in languages){
if(typeof languages[property]===“string”)
console.log(languages[property]}


#2

the dot notation:

languages.property

only works for properties, but the for in loop stores the properties as string in loop iterator, and for strings (or variable like the loop iterator which contains strings) we need to use square brackets notation

language."english" // doesn't work
langues["english"] // works

#3

ok,thank you for your explanation.


#4

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