Home > Software engineering >  How to convert NaN value to 0 inside app.post in .js file
How to convert NaN value to 0 inside app.post in .js file

Time:02-13

In below code question1 value is coming as NaN. I want to convert it to 0 but I am not able to do it using parseInt. How can I do this?

app.post('/auth/total',function(req,res){
    console.log(req.body);
    const {  question1, question2, question3, question4} = req.body;
    let total1 = parseInt(question1)   parseInt(question2) 
    let total2 = parseInt(question3)   parseInt(question4) 
    
    //some code to insert data in db and render the view 

});

CodePudding user response:

you can do this:

let total1 = (parseInt(question1) || 0)   (parseInt(question2) || 0)

this will convert any falsy value to 0

  • Related