0

I'm creating totals for a row in a table with JavaScript. The cell values are typed as strings, so a += would concatenate a delta. Is there anything that lets me cast this value in one line, so I can still use += without saving the old value in a old = Number(value) in an extra lin of code?

row.totals.value += delta;
[string]            [Integer]
Basti
  • 543
  • 1
  • 10
  • 20

3 Answers3

1

Unfortunately it won't be possible to do it without a casting of the LHS in conjunction with +=.

You'd also probably want to be using parseInt() or parseFloat() rather than Number(). Further reading.

alex
  • 460,746
  • 196
  • 858
  • 974
1

I'm not certain you can +=, but you should be able to use this on one line:

row.totals.value = Number(rows.totals.value) + delta;
Max
  • 191
  • 1
  • 3
1

You can do it in the following way

let str = '123';
str = +str + 4;
console.log(str);
marvel308
  • 9,972
  • 1
  • 18
  • 31