var obj = {a : 2};
console.log(obj.b); //prints undefined
console.log(b); //ReferenceError
Here both obj.b and b are not defined. Can anyone explain the reason behind the different outputs?
var obj = {a : 2};
console.log(obj.b); //prints undefined
console.log(b); //ReferenceError
Here both obj.b and b are not defined. Can anyone explain the reason behind the different outputs?
The first is a missing property, the second is a missing variable.
See also the difference here:
console.log(window.b);
console.log(b);
You can make the first part work by this
var obj = {a : 2};
console.log(obj.a); //Your property `b` was not defined or you typed `b` instead of `a`
..and the second part by this
var b = 3;
console.log(b);// `b` was not defined at all
Explanations in the comments