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:
JavaScript
x
10
10
1
const isOptionalChainingSupported = () => {
2
try {
3
eval('const foo = {}; foo?.bar');
4
} catch {
5
return false;
6
}
7
8
return true;
9
}
10