Creating an object


#1

Why does this only give my age and not my name also?

var myObj = new Object(); {
myObj.name = "Jason";
myObj.age = 26;
}


#2

Why does this only give my age and not my name also?

What do you mean? Your code works as expected. You can delete the braces, they are unwanted here, but even with them - your code works. Example -> https://repl.it/CoTs/0


Edit

Oh, a short explanation if you are worried about the output in the codecademy window. This is the common feature of the REPL's (read - eval - print loop) and it's very helpful (in fact).

Let's say that you have a JavaScript console in front of you and you simply want to make some calculations (you are a programmer, a calculator is not for you). So you just type (>> is the input indicator):

>> 28 * 2
=> 56

Oh, you got the result! JavaScript returns the value of the last evaluated expression. Great, right? :slight_smile:

But what is the last evaluated expression in your code? This line:

myObj.age = 26;

This is the assign operation, what is result value of it? Assigned value. In this case -> 26. That is why you see this number in the output window, but it does not mean that there is something wrong with the myObj.name.


In the normal environment, the return value of the last evaluated expression is marked by the => (or similar) indicator.

For clarification -> Codecademy is not a normal environment.


#3

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