Home > Net >  Js) what will be the simplest way to check null or '' as false but not 0?
Js) what will be the simplest way to check null or '' as false but not 0?

Time:12-22

I usually used if(value) to check value is null or not. But now I know false or 0 can be false. So I used if(value == null) but in this time, that null doesn't contain '' like. I want to check that 0, false(have specific value) as true, and I want to check that '', undefined, null as false What will be good way to check these values simply? Is there something to notice bout like this more?

CodePudding user response:

Just check for the possible values normally.

const isFalsy = param => (param === null) || (param === "") || (typeof param === "undefined")

isFalsy(null); // true
isFalsy(0); // false

I want to check that 0, false(have specific value) as true.

if(!isFalsy(value))

CodePudding user response:

const checkFalse = (value) => {
 if(value === '' || value === undefined || value === null)
 return {error: 'your error you want to print'} //if you want in return form
 throw new Error('your error you want to throw') //if you want to throw error
}
  • Related