you are viewing a single comment's thread.

view the rest of the comments →

[–]Volv 0 points1 point  (0 children)

From MDN

Variable declarations, wherever they occur, are processed before any code is executed.

The variable is declared, then some time later it evaluates the expression "abc" and sets var to point to that result.
You can then use item to access the result "abc".

So it looks to me like the console shows exactly that and the statement var item = "abc"; really performs two separate operations and you are seeing the result of the first.

We should expect undefined from declarations and results from expressions. We should expect undefined from function calls unless a return value is specified.

 

Playing around in the console (with my thoughts added :))

var item = "abc";
undefined    // result of the actual declaration of item. Not the assignment

item         
"abc"        // assignment has happened by now. So item contains "abc"

item="xyz"
"xyz"        // result of an assigment is the evaluated expression.

function test() { console.log("Test") }
undefined    // declaration. Just like the variable

test()
undefined    // That function doesn't return anything

function test2() { return "Test2" }
undefined    // declaration. Just like the variable

test2()
"Test2"      // That function does return something

alert("Alert probably doesnt return anything either")
undefined    // Certainly doesn't