My intent is to take the value from an object and pass it to other or use a default value if key doesn't exists. They use the operator ||
, but it just do not work for me.
When I try to compile the snippet on code section in an online coding platform: https://www.programiz.com/javascript/online-compiler/, it throws throws an error:
Code:
const object = {
"a": 1,
"b": 2
};
console.log({
"a": object?.a || 2,
"b": object?.b || 4,
"c": object?.c || 6
});
Error:
"a": object?.a || 2,
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47