Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an javascript boolean element is present

At this point, I check if elements in an node.js object exist and get the value by doing:

if(req.body.name) item.name = req.body.name;

However, if I want to do the same for a boolean element, it is not working. If the passed boolean is true, no problem, but if the passed boolean is false, the if statement fails, however, I need the value of it:

if(req.body.active) item.active = req.body.active; // works only if true

Is there a better way to check if an element in an object exists and to validate the boolean value of it?

like image 646
stijnpiron Avatar asked Dec 23 '22 15:12

stijnpiron


1 Answers

A few, you probably want in:

if("active" in req.body)
like image 176
Jonas Wilms Avatar answered Dec 28 '22 10:12

Jonas Wilms