Skip to content
Advertisement

Check if optional chaining is supported

I want to use a polyfill for optional chaining but I do not want to provide a polyfill for browsers which already support this feature.

Is there a way to determine whether a browser supports optional chaining ?

Advertisement

Answer

This works:

const isOptionalChainingSupported = () => {
  try {
    eval('const foo = {}; foo?.bar');
  } catch {
    return false;
  }

  return true;
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement