See http://jsfiddle.net/FDhQF/1/ for a trivial example.
What’s the difference between something being undefined and something not being defined in Javascript? For instance, trying to access a property for an object (effectively, trying to access a variable) that isn’t defined will return undefined. But you can also set something = undefined. When you do that, trying to access it still return undefined, but the pointer is still there. An example, as above, is how iterating over an object still goes over the property that you’ve (re)declared as undefined. It seems like there are two different sorts of undefined. Can anyone shed some light on the situation?
Both, accessing a property that isn’t defined on an object and a property that contains the primitive
undefinedvalue, will return youundefined.For example:
The difference is that
ais an own property, andbisn’t:In the first case
ais an own property, even if it containsundefinedas its value. In the second case,bis not an own property, accessingobj.bwill look for a property namedb, all way up in the prototype chain.When the prototype chain ends (when it reaches an object with a
null[[Prototype]]), the property lookup ends andundefinedis explicitly returned.You should know that the
hasOwnPropertymethod checks only if the property physically exist on the object (own properties), but we have also inherited properties, for that case we can use theinoperator, for example:As you can see,
objinherits fromTest.prototype, and theaproperty is not an own property, but it is available through the prototype chain. That’s whyhasOwnPropertyreturnsfalseand theinoperator returnstrue.You can see how internally properties are resolved by the
[[Get]]internal operationNotes:
undefinedas an identifier is not considered to be safe on ECMAScript 3 (the most widely implemented version of the language standard), because instead of being a language keyword (such asnullfor example) is just a property of the global object, and it is writable on this version of the Spec., meaning that if someone replaces its value (e.g.window.undefined = 'LOL';) , it will break your code.The
typeofoperator as @strager mentions can be used instead, for example:This operator returns always a string (it’s safe to use
==:), and its value depends of the type of its operand, the possible values are described here.Another common way to solve this is to declare your own
undefinedvariable, available on the scope of your functions, for example, some libraries use the following pattern:The function has an argument named
undefined, and it is executed immediately without passing any value to it (the last pair or parens make the invocation).Maybe is worth mentioning that the
undefinedglobal property was finally described on ECMAScript 5 as non-writable (immutable, as well non-enumerable and non-configurable -non deletable-).Using the
hasOwnPropertymethod directly from an object instance is also not considered as safe, because if some object has a property with the same name, the original method will be shadowed. For example:If you call:
The method defined on the object will be executed (and you wouldn’t want this since you know exactly which method you want to invoke…), because it shadows the one from the
Object.prototype, however it can be safely invoked by: