If the property we want to check in the object, whether it exists or not, even if it is undefined.
we will use one of them: 'prop' in obj (for checking properties from the prototype chain) or obj.hasOwnProperty ('prop')
we need to use the methods described above to check if a property exists as an access property that was not declared in the object, also returns undefined.
var o={}; oc=undefined; oc===undefined; //is true oa===undefined; //is true as well even though c exists while a doesn't
usually not a problem, since no one really declares undefined properties, but when this is done so.
oc=''; //when it can be string or oc=null; //to clearly indicate that its nothing. then oc === undefined will return false! note!!! null == undefined //true while null === undefined //false that why use three equals to test
For variables not declared and not inside the object. On access, the Compiler returns (link) an error. If this does not mean that it is regarded as a global property, the window object property has not been declared, at least in the entire parent scope, so it will be undefined just like oa was at the top. it will become window.prop .
so x;
To prevent properties from becoming a global variable, we use the var keyword inside a function , for example var k;
One thing you can do about this is to catch a reference error when you throw for a variable that does not exist and is considered the variable itself.
try { x } catch(e){//code to run when x is not declared let alone defined.}
source share