最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simplify checking "null or empty" for multiple variables in IF statement - Stack Overflow

programmeradmin8浏览0评论

I have two string variables in validation function which should ensure that variables won't be null or empty at the same time (also the case when one is null and other equals to empty, is not allowed).

Current IF statement is very plex as I have to check:

  1. if both are null
  2. if both are empty (if their lengths are equal to 0)
  3. if string1 is null and string2 is empty
  4. if string1 is empty and string2 is null

It is functional but ugly and not "best practice". How can it be simplified?

if ((string1==null && string2==null) || (string1.length==0 && string2.length==0) || (string1==null && string2.length==0) || (string2==null && string1.length==0))

I have two string variables in validation function which should ensure that variables won't be null or empty at the same time (also the case when one is null and other equals to empty, is not allowed).

Current IF statement is very plex as I have to check:

  1. if both are null
  2. if both are empty (if their lengths are equal to 0)
  3. if string1 is null and string2 is empty
  4. if string1 is empty and string2 is null

It is functional but ugly and not "best practice". How can it be simplified?

if ((string1==null && string2==null) || (string1.length==0 && string2.length==0) || (string1==null && string2.length==0) || (string2==null && string1.length==0))
Share Improve this question edited Mar 21, 2017 at 9:15 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked Mar 21, 2017 at 8:38 user3681549user3681549 2
  • 1 You could make use of type coercion and falsy values: if (string1 && string2) { ... } – Andreas Commented Mar 21, 2017 at 8:43
  • Put scenario aside, I gave enough info and I hope my question is clear. Just need best practice approach to simplify this long if condition. – user3681549 Commented Mar 21, 2017 at 8:50
Add a ment  | 

4 Answers 4

Reset to default 2

I think the best solution would be to check these conditions in order. First check against null then check if the strings aren't empty and to do it the best way would be to pare the string to an empty string which will avoid getting Exceptions if you use .length on undefined.

So use if(string1 === '') instead of writing if(string1.length==0).

Solution:

Then you can simply use the following condition:

if (!string1 || !string2 || string1 === '' || string2 === ''){
     //Your code here
}

Explanation:

It will be true if one of the two strings is null or empty, in other words if any of the four conditions is true it will enter the if block.

You could check one variable first if null or empty and than the other and join both result with AND.

if ((string1 === null || string1 === '') && (string2 === null || string2 === '')) {
    // both null or empty
}

If you have only other truthy values, you might use

if (!string1 && !string2) {
    // both falsy
}

You could use a function for readability, like:

function isEmpty(param){
  if(param==null || param.lenght==0)
    return true;
  else
    return false;
}

var string1, string2;

//And you if bees:
if(isEmpty(string1) || isEmpty(string2)){
  //[...]
  console.log('String1 & String2 are empty');
}

I hope it helps you, bye.

Maybe write a helper function:

function stringIsNotValid(val) {
    return val == null || val.length == 0;
}

if(stringIsNotValid(val1) && stringIsNotValid(val2)) { ... }

But a simplified if could be

if((string1 == null || string1.length == 0) && (string2 == null || string2.length == 0))
发布评论

评论列表(0)

  1. 暂无评论