I ‘ve inspected different behavior for undefined/undeclared variables in javascript .For example :
var obj = {};
console.log(x);//Error
console.debug(obj.x) ;//undefined
My question is though c and obj.x both are not declared or defined then why I’m getting diff behavior?
Am I missing something?
How should I track which variable is already exist or not?
You can’t reference an undeclared variable without it being an error, unless you are assigning it in non-strict mode then it becomes an implicit global. Still error in strict mode though.
Trying to access object properties is not the same as trying to access variables, though you can access global variables from
window:You need to use
window.xvsxin this situation for example: