I have a variable that gets defined by user input. I want to replace its value only if it's undefined. But not if it's NaN. How can I do it?
I tried doing x || 0 but that also replaces NaN values.
For ES6 users you can simply do:
x ?? 0
?? is a Nullish coalescing operator:
a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With