I’ve just taken over a project from another developer and the code has a lot of if statements like this:
if( value != null || value != "" || value != undefined )
{
doSomeThing();
}
Which while ok, does get a bit messy when used everywhere. Is there a better, neater way to write this? I was thinking of this
if( value.length > 0 || value != undefined )
{
doSomeThing();
}
What do you all think?
Thanks
Stephen
It’s nearly that, but with
if( value.length > 0 || value != undefined )when value isnullvalue.lengthwill thhrow a runtime error TypeError: Error #1009 as you are trying to access a property on anullvalue.so you have to check before that the value is non null as for example :
if (value!=null&&value.length>0) {doSomeThing();}or another form shorter :
One note:
Unless the type of value is
*, it can’t take the value undefined, it will benull.