I have small doubt in difference of local variable and global variable in browser console, when i type var a = 5 and hit enter in chrome browser console it returns undefined but when i type a = 5 not returning anything.
2 Answers
This has nothing to do with local vs. global variables. It's just that what the console shows you is the result of the statement. var statements don't have a result, so you see undefined. Assignment statements1 do have a result (the assigned value), so you see that value.
If you did
var a = 5; a ...you'd see undefined followed by 5.
1 Technically, it's an expression statement containing an assignment expression.
Comments
In my understanding when you write a=5, this will create a global variable. So it would be a part of window object. Hence you get its value.
When you do var a = 5, a new variable(reference) will be created, and then value will be set. Hence, you will get undefined.
You can even try following code for testing.
var a = {}; a.b = 5; This will return 5 and not undefined.