11/17 Question


#1

Hello,

Why does my code pass but only has 20 appear in the console?

var me = new Object();
me["name"] = "Karl";
me.age = 20;


#2

Hi @kderrick1988

It appears in the console because when you put a value in a variable, she returns her value.


#3

I guess the explanation was something like this. The console buffers each new value until it is either used or replaced with another one and if it reaches the end of your code with a value still in the buffer (not undefined -> empty) it will echo this value. So as an assignment statement has the value you assign here me.age = 20; has a value of 20 you see 20. You could prove that by printing it to the console -> console.log(me.age = 20);. Also if you want to see stuff on the console and not just a default echo better use console.log anyway. It also emptys the buffer as it uses the value so it also get's rid of the extra output if it is the last statement.


#4

Is that only when the value is part of an object? Because that doesn't happen any other time.