The window &&
check at the start of each of the checks is unnecessary. There's never a natural case where that will evaluate to false. (If you run the code in Nodejs or in a web worker where the window
global isn't present, then the line will throw an exception, and not evaluate to false. There's no environment where window &&
will improve things, and having that there is misleading.)
#1 will check whether the window
object has a navigator
property with any value, etc. #2 will check whether the window
object has a navigator
property with a truthy value. Unless you expect window.navigator
or window.navigator.userAgent
to be present as properties but be set to false
, null
, undefined
, 0
, NaN
, or ''
(which those specific properties aren't ever naturally in browsers), then #2 will work just fine and it's nicely shorter.