2
function doIt()
{
    var person={firstname:"John", lastname:"Smith", age:"25"};
    var x;
    var txt="";
    for (x in person)
    {
        txt=txt+person[x] +"<br>";
    }
    document.getElementById("showtext").innerHTML=txt;
}

My question is: Why when I replace

txt=txt+person[x]+"<br>";

with:

txt=txt+person.x+"<br>";

the value of person.x is returned as undefined? In the first iteration of the loop, x should be 'firstname'. So person.x should be equal to person.firstname, and thus return the value John. I would love to understand why it returns 'undefined' instead.

1
  • 1
    Because of javascript. Commented Feb 26, 2013 at 15:12

6 Answers 6

3

In the first case you're using 'bracket notation', where the value of the variable x is used to determine the property name.

In the second case you're using 'dot notation', where the property looked for is literally called x.

Sign up to request clarification or add additional context in comments.

Comments

3

The Answer is:

Since x ist not the property name and the object doesnt have a property with the name/key x.

person.x

is undefined.

This would be equivalent to

person["x"] (the subtle difference lies in the double quotes)

what is also undefined.

For it to work with the dot-notation, you would have to write :

 eval("person." + x); // but this is evil 
 // Tested on win7 with chrome 45+

so expression eval("person." + x) would expand in the first run to eval("person.firstname" ) which returns "John"
...

What I don't recommend, because eval can introduce may security issues.

Update 1

Disclaimer: With this answer i only answered the initial question, and tried to explain the problem. With "// but this is evil " i am suggesting not to use this approche.

3 Comments

Not only eval but also completely unnecessary.
It is just to make a point, that x is a String and not the name of the parameter, and mainly to answer the question to the full extend. :)
It is not unnecessary it was an answer to the question, an explanation why the things are how they are(what the initial question was). I was trying to teach fishing, not just hand over a fish.
2

When you write person[x], it means "look up the value of x, and then find that element in person". When you write person.x it means "look up the value of x inside of person".

person doesn't have an x element, so you're getting undefined. You really do just want person[x].

Comments

0

x will be a string. eg "person" so you have to use [] brackets

Comments

0

You can't use dot notation with a variable key. It will look up the property "x" which is undefined. person[x] is the right way.

Comments

0

Javascript will allow you to access an object's property using a variable if you use the square brackets syntax. Thus, person[x] will do what you are trying to do as long as x contains a string representing the property name. The syntax construction person.x is equivalent to person["x"].

1 Comment

Thank you all for your quick answers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.