-1

I thought my object detection would work:

if (Notification!=undefined) {}

However my JavaScript log is still showing the following error:

Uncaught ReferenceError: Notification is not defined

How do I properly do object detection for the Notification object?

No frameworks.

John
  • 11,516
  • 11
  • 87
  • 151

1 Answers1

6

You can use typeof like so:

if (typeof Notification !== 'undefined') {

}

If you use typeof, it does not try to actually use the variable (which breaks and throws an error)

Tuvia
  • 794
  • 3
  • 15