Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullish coalescing assignment operator (??=) in NodeJS

I'm trying to use the Nullish coalescing assignment operator (??=) in NodeJS, it is possible?

const setValue = (object, path, value) => {
  const indices = {
      first: 0,
      second: 1
    },
    keys = path.replace(new RegExp(Object.keys(indices).join('|'), 'g'), k => indices[k]).split('.'),
    last = keys.pop();

  keys
    .reduce((o, k, i, kk) => o[k] ??= isFinite(i + 1 in kk ? kk[i + 1] : last) ? [] : {}, object)[last] = value;

  return obj;
}
est.js:9
       .reduce((o, k, i, kk) => o[k] ??= isFinite(i + 1 in kk ? kk[i + 1] : last) ? [] : {}, object)
                                      ^

SyntaxError: Unexpected token '?'
        at wrapSafe (internal/modules/cjs/loader.js:1067:16)
        at Module._compile (internal/modules/cjs/loader.js:1115:27)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
        at Module.load (internal/modules/cjs/loader.js:1000:32)
        at Function.Module._load (internal/modules/cjs/loader.js:899:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
        at internal/main/run_main_module.js:17:47
like image 418
Hamada Avatar asked Feb 19 '26 20:02

Hamada


1 Answers

The error means your version of Node does not yet have support for the ??= operator.

Check out the versions which support it on node.green:

enter image description here

like image 121
trincot Avatar answered Feb 21 '26 10:02

trincot