79284985

Date: 2024-12-16 14:28:23
Score: 0.5
Natty:
Report link

No it's not exactly equivalent.

Difference:

With ({foo = 20}), default value is used if bar.foo is undefined, but not if bar.foo is null.

const {foo = 20} = bar

With (??), the default value is used for null or undefined.

const foo = bar?.foo ?? 20

let bar = { foo: null };
const { foo = 20 } = bar;
console.log(foo); // null (not 20)

bar = { foo: null };
const fooVal = bar?.foo ?? 20;
console.log(fooVal); // 20

Reasons:
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Error