EXPLANATION
case x = 10 :
This creates a variable in a global scope named x with a value of 10 . It is also an expression that returns 10 . This is useful in order to be able to do things like var x = y = 10; which sets both x and y to 10
case var x = 10 :
This creates a variable in the current scope, which is exactly what happens to the global scope named x with value 10 . Because it was created using the var syntax, it cannot be evaluated as an expression, so it returns undefined , which is printed to the console.
SUMMARY
There is no difference in writing var x = 10 vs x = 10 from the console, although it will be in other places. The latter is also prohibited in strict mode. However, the first returns undefined because there is no exit at run time, however the second returns 10 because x=10 is an expression.
Example
You can see what happens a little better if you use eval
var output = eval('x = 10'); console.log(output)
vs
var output = eval('var x = 10'); console.log(output)
source share