-1

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 for undefined and null by way of null. The use of == null is also acceptable in cases where only one of null or undefined may be logically encountered, such as uninitialized variables.

skube
  • 5,867
  • 9
  • 53
  • 77

1 Answers1

0

I reckon you'd use obj.hasOwnProperty(prop)

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

Adam Hartleb
  • 465
  • 1
  • 6
  • 12