Why it works only with languages[x] not also with languages.x


Please look at If arguments in the code. Why it is not working if I change with if (typeof languages.x)

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(x);



because your for in loop stores the values in x as string

this works:


this doesn't:


because x contain string values associative arrays notation (square brackets) must be used


yes your example exactly what I tested. I did not know that x in for loop stores string.
That figures. Thanks a lot.


if you really want to dive deeper into this, you have to read something like this:


to understand how objects and associative arrays are the same, JS has this very different implemented then many other languages


Thanks a lot stetim94. Really appreciate that.


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