6

Is there a succinct operation in JavaScript (ES2015) that does this:

x => x === undefined ? 0 : x

I keep running into situations where I want to increment x, but it's initially undefined, so the code becomes:

foo.bar = (foo.bar === undefined ? 0 : foo.bar) + 1;

or

foo.bar = (foo.bar ? foo.bar : 0) + 1;

Is there a less repetitive way of doing this cast?

asked Jan 4, 2017 at 2:57
1
  • 3
    Did you try (foo.bar || 0)? Commented Jan 4, 2017 at 2:59

4 Answers 4

16
foo.bar = (foo.bar || 0) + 1;

should work.

answered Jan 4, 2017 at 3:04
Sign up to request clarification or add additional context in comments.

Comments

7

ES2020 and the nullish coalescing operator allows to simplify it:

foo.bar = (foo.bar ?? 0) + 1;
answered Jul 10, 2020 at 15:19

5 Comments

Though this isn't necessarily any simpler than just a simple OR, as above
If foo.bar = NaN, the expression with an OR can provoke undesired results. The output of the nullish colaescing operator expression would be NaN as well. However, with a simple OR, you would get 1 as a result. @rubie
Yeah I guess that's desired or undesired based on context, but presumably if you're trying to cast foo.bar to a number, it's because you want to avoid NaN in most cases.
let _a = a(); let _b = b(); Imagine that a() and b() both return 0. Now you do: foo.bar = _a/_b. You didn’t desired a NaN. You got it based on a division of two functions where you didn’t know the result beforehand.
Ah interesting, very good example. In the end, ?? and || aren't doing the same thing here, so there are going to be different use cases. The third answer is also interesting, but also has a big gap.
3

Since undefined + 1 is NaN, which is falsy, and undefined is falsy, you have a few options.

Assuming your numbers will never be negative (specifically, -1)

foo.bar = foo.bar + 1 || 1

Or this works for any values of foo.bar, though David's answer is probably a better idea.

foo.bar ? foo.bar++ : foo.bar = 1
answered Jan 4, 2017 at 3:18

Comments

1

You could use this succinct alternative using the double tilde (~~) as the bitwise NOT operator.

foo.bar = ~~foo.bar + 1;

As @CherryDT helpfully points out:

this will cause undesired results outside of the signed 32-bit integer range (~~30000000000 is -64771072 for example)

answered Sep 24, 2024 at 17:52

2 Comments

I wanted to cite where I discovered that approach: stackoverflow.com/a/69646204/264498 Not a good answer for that question but helpful for this scenario.
Note, however, that this will cause undesired results outside of the signed 32-bit integer range (~~30000000000 is -64771072 for example).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.