If you coerce that string to a boolean then it should check all your conditions, which is pretty much checking if user.phone
is truthy.
It depends how you want to use it. If you wanted to use it in a condition, i.e. if(userHasPhoneNumber) ...
then you can use the string directly : if(user.phone)
as it will coerce to a boolean.
If you really need to have a boolean variable then need to cast it to a boolean explicitely:
Either through
const userHasPhoneNumber = Boolean(user.phone);
or
const userHasPhoneNumber = !!user.phone;
Note, as @Bergi commented, that there are more values that are coerced to a false value (falsy values), for example NaN
or the number 0
(the string "0" will coerce to true), so it depends what your input is. If it's never a number but either a string/boolean/null
/undefined
, it should be fine. Here is the list of all falsy values for reference : https://developer.mozilla.org/en-US/docs/Glossary/Falsy