"Looks For-In To Me" Dot vs Bracket notation


#1


Why is it that when I wrote

  var langvalue = languages.lang;

The code does not work? I read somewhere that dot notation means literal value while bracket can read variable-- but I don't really understand what that means. What is literal value?


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

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


#2

literal value in this case means english, french and so on. Literally the literal value of the object properties, look:

console.log(langauge.english)

however, due to your for loop lang is a variable, so you need to use square brackets


#3

So the literal value of English isn't "Hello?" What is it then?


#4

english is the literal value. "Hello!" is the value associated with this literal value


#5

You're probably referring to that these two expressions are equivalent:

myObj.attribute
myObj['attribute']

And:


#7

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