How to convert NULL to 0 in JavaScript

 

There are many ways to deal with this. The one you describe, adding an integer value to coerce the type, is fine. You can also explicitly convert to a number:

 

let num = null;
num  = Number(num);
//or
num = +num;
//or
num = num || 0

Tags:

Share:

Related posts