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
  • 606
  • 1
  • 12
  • 22

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
  • 479,566
  • 201
  • 878
  • 984
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
  • 216
  • 2
  • 3
1

You can do it in the following way

let str = '123';
str = +str + 4;
console.log(str);
marvel308
  • 10,288
  • 1
  • 21
  • 32