Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

What is the shortest way to check if two or more variables are equal to 0 in JavaScript?

I'm curious to know if there's something shorter than if(!x&&!y&&!z){...}.

share|improve this question
1  
This might be better suited for StackOverflow or CodeReview. –  Kyle Kanos Jun 6 at 13:04
    
If you want the shortest solution in bytes in order to improve golfing down this particular snippet, I think this is well on-topic here (not as a challenge, but as a question asking for golfing advice). If you want the fastest solution, this should rather go in Code Review (and no one prevents you from posting both separately). But the shortest solution will not necessarily be the fastest solution. So what is it you want? –  Martin Büttner Jun 6 at 13:10
1  
wouldn't !(x|y|z) work? –  Danny Jun 6 at 13:12
2  
Questions on this site should either the contests or asking for tips participating in those contests. Do yourself a favor and don't use any code you find on this site in "the real world", unless you understand exactly what it does. Also, in general, you have to decide between the shortest and the fastest solution. –  Dennis Jun 6 at 13:26
2  
Seeing my answer was apparently what you were looking for, I took the liberty to edit your question to make it more on-topic (and justify my subsequent reopen vote). Feel free to edit the question again if you don't agree with my changes. As stated before, for help with efficiency, try StackOverflow or Code Review. –  Martin Büttner Jun 6 at 13:39

3 Answers 3

(EDIT: This first part refers to the original phrasing of the question.)

First, (!x&&!y&&!z) returns a boolean, which makes ?true:false entirely redundant. It's basically like using

if (x == true)
  return true;
else if (x == false)
  return false;

instead of return x;.

That gives you

!x&&!y&&!z

(EDIT: The remainder still applies to the new version of the question.)

Now you could apply De Morgan's law. The above is equivalent to

!(x||y||z)

Which is the same length for 3 variables and longer for 2 variables. But for each variable beyond the third, you save one more character!

Lastly, if you know that your variables are numbers, you can also use the bitwise operator, i.e.

!(x|y|z)

If you actually need booleans (which I assume from your snippet), this doesn't work for the & case, because !x&!y&!z will give you an integer. Then again, in JavaScript it's often enough to have truthy or falsy values, in which case, that's a perfectly valid option.

Bonus tip: if you ever want to turn a truthy/falsy value (like a non-zero/zero number) into a boolean, don't use x?true:false either, but use !!x instead. !x is a boolean with the opposite truthiness/falsiness than x, so !!x is true for x truthy and false for x falsy.

share|improve this answer
1  
Very insightful! I appreciate you exploring several possibilities, despite the nature of the question being somewhat vague and potentially misinterpretable. –  purefusion Jun 6 at 13:40

This function is obviously not the shortest until you have a lot of numbers to check (the poster did say "or more"). It verifies that all arguments are equal to zero.

function AllZero() {
    var args = Array.prototype.slice.call(arguments);
    args.push(0);
    return Math.min.apply(Math,args) === Math.max.apply(Math,args);
}

If you push a different number in the args.push(x) line, it will check that all of the arguments are equal to that number.

share|improve this answer
    
Why would you recommend this instead of a simple loop? Also AllZero('', '', '') –  nderscore Jun 7 at 0:15

In pure code golf terms multiplication is shorter:

if(x*y*z){}

Which works since (in most browsers) 0 is false and anything else will work as true.

But I expect from a performance perspective the other answer is superior.

share|improve this answer
    
You can safely scratch in most browsers. The specification of ToBoolean() clearly states that the only falsy Numbers are +0, -0 and NaN. –  Dennis Jun 6 at 18:19
2  
Doesn't this check if any of them are zero, not all of them? The only way this is true is if all are non-zero, and any single zero gives false. I assumed he was looking for true if all variables were zero, and false otherwise. –  Geobits Jun 6 at 18:23
    
You can use addition if the numbers are all guaranteed to be >-1 or <1. –  undergroundmonorail Jun 6 at 18:44
1  
@undergroundmonorail: That still requires negation though. Or this shorter alternative: x+y==-z –  Dennis Jun 6 at 18:46
    
This tests if none of the values are 0 –  nderscore Jun 6 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.