admin管理员组

文章数量:1405577

I am trying to make a function which check the value if null or ""

Here is what I got so far (that is working)

function notnull(values,success,failed){
var count = 0

$.each(values,function(index,val){
    console.log(val)
    val != "" && val != null ? count++ : count 
})
if(count == values.length){
    return true;
}else{
    console.log('false')
}
}

However If I tried short circuiting the 2nd if statement it returns an error

function notnull(values,success,failed){
var count = 0

$.each(values,function(index,val){
    val != "" && val != null ? count++ : count
})(count == values.length) ? return true:console.log('false')
}

the error says

Uncaught SyntaxError: Unexpected token return

Question 1: How can I return true or false in a short circuit version

Question 2: at the code

val != "" && val != null ? count++ : count

How can I omit the else part?

I am trying to make a function which check the value if null or ""

Here is what I got so far (that is working)

function notnull(values,success,failed){
var count = 0

$.each(values,function(index,val){
    console.log(val)
    val != "" && val != null ? count++ : count 
})
if(count == values.length){
    return true;
}else{
    console.log('false')
}
}

However If I tried short circuiting the 2nd if statement it returns an error

function notnull(values,success,failed){
var count = 0

$.each(values,function(index,val){
    val != "" && val != null ? count++ : count
})(count == values.length) ? return true:console.log('false')
}

the error says

Uncaught SyntaxError: Unexpected token return

Question 1: How can I return true or false in a short circuit version

Question 2: at the code

val != "" && val != null ? count++ : count

How can I omit the else part?

Share Improve this question asked Jan 30, 2018 at 4:59 Vian Ojeda GarciaVian Ojeda Garcia 8674 gold badges17 silver badges37 bronze badges 5
  • What's wrong with the first approach? What you are trying in the 2nd is pletely invalid syntax. return can only be the first keyword in a statement. – marekful Commented Jan 30, 2018 at 5:05
  • 1 function notnull( values, usused1, unused2 ) { return values.filter( x => x ).length !== 0 } – Tibrogargan Commented Jan 30, 2018 at 5:05
  • and if you really wanted one that checks if anything at all is null: function anynull( values ) { return values.filter( x => x ).length !== values.length } – Tibrogargan Commented Jan 30, 2018 at 5:07
  • The above depends on the JavaScript behavior of filter to test the return value of the filter function for "truthiness". "", 0, false, undefined, NaN and null are all "not truthy" – Tibrogargan Commented Jan 30, 2018 at 5:08
  • another issue with your code, do you understand what $.each() returns? it's not a function, so, you'd get an error at runtime anyway – Jaromanda X Commented Jan 30, 2018 at 5:15
Add a ment  | 

1 Answer 1

Reset to default 3

Your second example is a bit messy, but if I understand your function, you want to go through each value and make sure none of them are null, right?

Here is your original, cleaned up a bit:

function notNull(values){
  let count = 0;

  $.each(values, function (index,val) {
    (val != "" && val != null) ? count++ : count 
  });

  return count == values.length; // returns true or false
}

You are basically going through each of the values, counting them up if they aren't null, and returning true if none were null (your count is the same as your array) or false otherwise.

You are using the jQuery equivalent of the vanilla each() function. They function the same way: they'll go through every single entry in an array.

You can't short-circuit each(). Instead, you need to use some() or every(). Those functions are similar, but opposite:

  • some() - Continues to loop through as long as its callback returns false.
  • every() - Continues to loop through as long as its callback returns true.

In your case, you want to use every() because you want to go through every element and make sure it is something (in your case, not null):

function notNull(values) {
  return values.every((value) => value != "" && value != null);
}

console.log(notNull([1,2,3]));
console.log(notNull([1,null,3]));

Much nicer. This will check each value. As long as they match the condition, it'll keep going and ultimately return true. If if finds one that does match, it'll short circuit there and return false.


As for your second question, how can you leave out the "else" part of this:

val != "" && val != null ? count++ : count

With the ternary operator (?:), you can't. But, you can with the normal boolean operators:

val != "" && val != null && count++;

JavaScript will short-circuit that condition at the first false, so it'll only get to count++ if the other two bits are true.


On your second example, I think you were attempting something like:

condition ? return value : console.log('a')

This would be an invalid syntax. You can only have values inside of a ternary. You could do something like this:

return condition ? value : otherValue;

If you want a return mixed in, you have to do it as two separate things:

!condition && console.log(''); // log if false;
if (condition) return value;

return and throw are both stubborn in this way and always have to be broken out and can't be mixed with other selective operators.

本文标签: Javascript short circuit if statement How to return trueStack Overflow