If I have some object that has a property that may or may not exist, is there a preferred way to check for its' existence?
// Good?
(someObj.property !== undefined && someObj.property !== null)
// Better?
(typeof someObj.property !== 'undefined')
// Best?
(someObj.property != null)
*The last !=
operator is on purpose:
Strict equality checks (
===
) must be used in favor of abstract equality checks (==
). The only exception is when checking forundefined
andnull
by way ofnull
. The use of== null
is also acceptable in cases where only one ofnull
orundefined
may be logically encountered, such as uninitialized variables.