The result of parseInt()
is going to be either a number or NaN
, so you can use ||
:
const my_v = parseInt(jQuery("#id").val()) || 0;
Now, in general I would caution anyone against using parseInt()
, because (by design) it accepts strings like "123hello world" as valid numbers (123
in this case). You can use the Number()
constructor or plain unary +
to coerce a string to a number. If you don't want a fractional part, you can use Math.floor()
or Math.round()
, and you can check numerically to see if there's a fractional part if that should be considered an error.