Does the nullish coalescing operator (??) in TypeScript have more browser support than JavaScript's?

2.8k views Asked by At

In JavaScript, browser support for the nullish coalescing operator (??) is limited to newer browsers (e.g. Chrome 80, Edge 80, Firefox 72). Since TypeScript is converted to JavaScript, do nullish coalescing operators go through some sort of conversion as well, sort of like a polyfill?

1

There are 1 answers

12
CertainPerformance On BEST ANSWER

do nullish coalescing operators go through some sort of conversion as well, sort of like a polyfill?

The TypeScript gets transpiled to JavaScript. As for now, yes, nullish coalescing will be transpiled, along with all the other syntax that isn't yet supported by your target ES version in your tsconfig.

For example, in TS:

obj.foo ?? 5;

gets transpiled to

"use strict";
var _a;
(_a = obj.foo) !== null && _a !== void 0 ? _a : 5;

Similarly, the exponentiation operator:

3 ** 5

gets transpiled to

Math.pow(3, 5);

if your target is ES2015 or earlier. (The exponentiation operator was introduced in ES2016.) Otherwise, if your target is ES2016 or greater, it does not get transpiled.