I recently tried to upload some of my svelte-kit code to a cPanel node.js app, but got an error saying that "??=" was an unexpected token. I was looking through the code, and found the line causing the error const component = async () => component_cache ??= (await import('./layout.svelte-32c85b96.mjs')).default;
. I was wondering how to replace this as the node.js version I can use is 14. I tried looking up "??=" on Google but found literally nothing.
Asked
Active
Viewed 103 times
0

codingtuba
- 366
- 2
- 13
1 Answers
1
What the ??=
(Nullish coalescing assignment) operator does is assign a value to the variable if it is undefined. For example,
let a; let loga = ()=>console.log(a);
loga() // => undefined
a ??= 1 // a is set to 1
loga() // => 1
a ??= 2 // a is not set to 2 as it is already 1
loga() // => 1
a = undefined
a ??= 2 // a is now set to 2 as it is now undefined
loga() // => 2
So, if you want to fix the error then you should probably just use a binary operator instead of a ??=
:
let a;
a == null ? (a = "new value") : ("")
// vs:
// a ??= "new value"

codingtuba
- 366
- 2
- 13
-
`??=` will assign it if the LHS is `undefined` _or_ `null`. So the equivalent would be to check if `a == undefined ? ...` – Nick Parsons Jul 13 '23 at 02:03
-
1Or, `a == null`, which is shorter. – Unmitigated Jul 13 '23 at 02:22
-
I've added @Unmitigated's suggestion. – codingtuba Jul 13 '23 at 02:25