3

So the ??= operator assigns the value to the variable only if the current stored value is nullish.

Maybe I'm missing the obvious but I can't think of a slick solution (without if statements) to only assign if the value is not nullish?

I'm using nodeJS to give a bit more context.


I want

let x r??= 2;
// Updates 'x' to hold this new value
x r??= undefined;
// Has no effect, since the value to assign is nullish
console.log(x); // 2

EDIT to give more clarity to my problem:

I want a variable only to be assigned a new value, if that new value is not nullish.

let iceCream = {
    flavor: 'chocolate'
}

const foo = 2.5
const bar = undefined;

iceCream.price r??= bar
// does not assign the new value because it is nullish
console.log(iceCream.price) // expected to be error, no such property

iceCream.price r??= foo
// assigns the new value because it is not nullish but a float
console.log(iceCream.price) // expected to be 2.5

iceCream.price r??= bar
// does not assign the new value because it is nullish
console.log(iceCream.price) // expected to still be 2.5
Proxycon
  • 71
  • 7
  • Doesn't `x ??= undefined` already do exactly that? – Ivar Nov 24 '21 at 20:48
  • @Ivar `x ?? = undefined` assigns `x` to undefined if `x` is nullish. OP wants to assign if `x` is **not** nullish. – Daniel Baldi Nov 24 '21 at 20:50
  • 1
    @DanielBaldi: I think it’s if the *new* value is not nullish. – Ry- Nov 24 '21 at 20:51
  • 2
    @Proxycon: Show the context where you’re going to use it for clarity and most appropriate alternatives? – Ry- Nov 24 '21 at 20:52
  • @Ry- which new value you're referring to? – Daniel Baldi Nov 24 '21 at 20:52
  • 2
    As I understand it (and as I think Ry is referring to), OP wants to set `x` to the right hand side operant, if the right hand side operant is not nullish (not when `x` is not nullish). Though my initial comment wont work, because it will still remain unmodified if both values are not nullish (if `x` is `2` and you use `x ??= 3`, it'll remain `2` instead of assigning `3`). – Ivar Nov 24 '21 at 21:03
  • 2
    Regarding "_expected to be error, no such property_", note that accessing non-existing properties of an object doesn't throw errors. It just returns undefined. (Accessing properties of the `undefined` value/property however does throw errors.) – Ivar Nov 24 '21 at 22:03
  • I believe you're looking for `x = newVal ?? x;`. Assigns `newVal` if `newVal` is anything except `null` or `undefined`, even falsy values (`0`, `NaN`, etc...). In all other cases, assigns `x`, which keeps it unchanged. For objects you can do `x.foo = newVal ?? x.foo;`, obviously. – tao Nov 25 '21 at 01:29

3 Answers3

3

No, that’s not a single operator. The closest is two operators:

x = undefined ?? x;
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Adding another answer after clarification as editing my previous one seemed weird.

The simplest way I can think of a solution without if is as follows:

let iceCream = {
    flavor: 'chocolate'
}

const foo = 2.5
const bar = undefined;
bar && (iceCream.price = bar)
// Another possible solution if creating the property with a nullish value is ok for you:
iceCream.price = bar || iceCream.price;
Daniel Baldi
  • 794
  • 1
  • 9
  • 1
    This is better than your other answer, but _non-nullish_ is not the same as 'truthy', and _nullish_ is not the same as 'falsy'. This distinction is **very** important to the question, since OP wants right-hand values such as `0`, `""` and `false` (which are evaluated as _falsy_) to be assigned. – gyohza Feb 05 '22 at 00:46
-1

You could use logical AND assignment.

From the MDN Web Docs:

let a = 1;
let b = 0;

a &&= 2;
console.log(a);
// expected output: 2

b &&= 2;
console.log(b);
// expected output: 0
Daniel Baldi
  • 794
  • 1
  • 9
  • But this operator also only depends on the value stored in the variable, not on the value that is to be assigned if I understand correctly. – Proxycon Nov 24 '21 at 21:32
  • This answer doesn't really address the issue. This only takes into consideration the left-hand side of the assignment. As explained in other comments, OP wants the assignment to take place if the **right-hand side** (i.e. the _new value_) is non-nullish. – gyohza Feb 05 '22 at 00:37