8man 8man - 10 months ago 60
Javascript Question

javascript how to set null input equal to 0

I have javascript function that will calculate the value for "TOTAL" row and "Balance" column in the table. But if there are no input, the javascript will return NaN which is not a number error.
How to set if there are no input in the text box, javascript will interpret it as 0 so that I will not get NaN error.

This is my table

As you can see, the textbox for pay(notes 50) column are empty. Then value for balance and total become NaN.

Answer Source

just use logical 'or' operator (see short-circuit evaluation),

parseInt('10') || 0; // give 10
parseInt('') || 0; // give 0