We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to validate empty in if condition

SK36
Tera Contributor

Hi all,

In catalog client script, when value is empty or null, how to deal in if condition?

eg:

Var val;                             //or val='' whatever. But it is empty

if(val != null || val !=''){    //its not working.

alert(val);

}

So, how to deal empty variable?

1 ACCEPTED SOLUTION

RaghavSh
Giga Patron

when you have two "NOT" conditions always use && between them instead of ||.

Try below:

Var val;                             

if(val !='' && val !=null){    

alert(val);

}


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

View solution in original post

2 REPLIES 2

RaghavSh
Giga Patron

when you have two "NOT" conditions always use && between them instead of ||.

Try below:

Var val;                             

if(val !='' && val !=null){    

alert(val);

}


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

Not applicable

Try below if condition if var is empty
if(!var){

//your code

}

 

 

Please mark as correct answer if this solves your issue.