Search code examples
javascripttypeof

Check if matchAll() is Defined in JavaScript


I'm using Chrome and executing a piece of code where matchAll() is used in JavaScript. However, I only want to use this piece of code if matchAll() is defined, which it is not for Internet Explorer 11. And so I put a check on this like the following:

if (typeof matchAll === 'function') {execute code using matchAll() }

For some reason though, this check is not working and I'm simply trying to execute code when matchAll is defined as a function. Any help would be appreciated. Thanks!


Solution

  • I expect you are looking for String.prototype.matchAll() rather than simply a function matchAll()

    You can check for this like so:

    if (typeof String.prototype.matchAll === 'function') { ... }