i-g
i-g

Reputation: 69

How to feature-check for three-equals-signs operator and promises support in JS?

I'm writing scripts which I want to split into several modules. The "baseline" modules will support older browsers, which do not support new syntax such as === and promises.

The "advanced" modules will be loaded if the browser passes a feature-check.

My question is, how do I check if browser supports === operator and .then(function(){}) promise syntax without actually using them first, and causing a syntax error in older browsers?

if (/*what goes here*/) {
    var script = document.createElement('script');
    script.src = '/advanced.js';
    script.async = false;
    document.head.appendChild(script);
}

Upvotes: 0

Views: 171

Answers (1)

Lonnie Best
Lonnie Best

Reputation: 11344

If a browser supports promises, it will support then. One way (among others) to see if a browser supports promises (without throwing an error) would be to see if window.Promise exists:

if(window.hasOwnProperty("Promise"))
{
  console.log("Promises are supported.");
}
else
{
  console.log("This browser does NOT support promises.");
}

As for ===, I don't think you'll have to worry about that one. === was added to ECMAscript in the 3rd edition in December of 1999 and it is hard to imagine anyone (even a diehard laggard) using a browser today that doesn't support it.

UPDATE:

If you really insist on detecting === support, my conclusion (from my comments below) is to accomplish this by researching which browsers do not support === and using Browser Detection to detect those browsers. I hope someone else offers you an easier way I'm not thinking of.

Upvotes: -1

Related Questions