I am using the following code to convert data attribute values to Numbers. I want to get a valid numeric value or 0 in all cases. e.g if the data attribute value is blank(“”) then I need 0 in that case.
I am using the following code to convert data attribute values to Numbers. I want to get a valid numeric value or 0 in all cases. e.g if the data attribute value is blank(“”) then I need 0 in that case. The intention is to avoid getting NaN because I am using the result in calculations.
Is this right way to convert data attributes values to number in javascipt?
If the data attribute is called val then yeah, that’ll do the trick.
Nowadays however, I tend towards making things more verbose, so that future me doesn’t have to spend as much brain power reading code I wrote previously: