1

I read that sometimes the && operator is used to "short circuit" JavaScript into believing that a return value of 0 is 0 and not NaN because 0 is a falsy number in JavaScript. I've been looking around to figure out what all this means. Can someone explain it to a layman?

For example:

function sign(number) {    
    return number && number / Math.abs(number); }

Will return 0 if number is 0.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 6
    Why would a return value of 0 be NaN? – Kerrek SB Oct 02 '11 at 15:23
  • 2
    provide us please source of your informations – Marek Sebera Oct 02 '11 at 15:24
  • 1
    @KerrekSB To understand what he's asking, read: http://stackoverflow.com/questions/7624920/number-sign-in-javascript/7624961#7624961 – NullUserException Oct 02 '11 at 15:26
  • 1
    Basically, if `number` is `0`, then it will evaluate to `false`, so the `&&` operator will *short-circuit* and immediately "return" `0`, without evaluating its second operand, which would produce `NaN` since `number` is `0` and `0/0` is `NaN`. – Frédéric Hamidi Oct 02 '11 at 15:35
  • 1
    I see now. This was a very poorly constructed question before the edit. – Kerrek SB Oct 02 '11 at 15:45
  • @FrédéricHamidi Ooops, brain fart. – NullUserException Oct 02 '11 at 16:07
  • This is going to sound a little silly, but I'm not a math wiz, really. I thought 0/0 was "unidentified". I've heard of people arguing against that for special and complex cases, but wouldn't "unidentified" be a more reasonable return value? It's still NaN, but surely there would be cases where unidentified would be more useful than NaN. – Coke Christopher Oct 03 '11 at 04:20
  • @Kerrek That's just the nature of questions. Without knowing the answer to a question, it's difficult to ask a question that speaks to the answer. It's hard to know if answerers are going to understand, or if they will need additional information, or whatever. Thanks for helping me find the right question. – Coke Christopher Oct 03 '11 at 04:24
  • 1
    @CokeChristopher: Well, perhaps, but you should strive to provide sufficient *context* for your question in any case. If you just say, "I heard a rumour about C++, is it true?", then it's hard for others to know what's going on in your mind. Anyway, I guess we've figured it out now :-) – Kerrek SB Oct 03 '11 at 10:20

3 Answers3

6

This sounds like an answer that I saw earlier, but cannot find now (a link would be helpful), but you've got the wrong end of the stick here.

Given a situation like:

foo = 0 ;
bar = foo && 2 / foo;

On line two, foo will be evaluated. It is 0 and therefore a false value. The left hand side of && (foo) will be returned and assigned to bar.

Now if we have foo = 1:

foo = 1 ;
bar = foo && 2 / foo;

Again, foo will be evaluated. It is a true value, so the right hand side of && will be evaluated and returned. 2 / foo is 2 so 2 is assigned to bar.

"Short circuit" just means that as soon as part of && fails then it returns the part the failed without evaluating anything to the right.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

In JavaScript, the boolean operators && and || don't necessarily return a boolean. Instead, they look at the "truthiness" of their arguments and might short circuit accordingly. Some values like 0, the empty string "" and null are "falsy".

Short circuiting just means skip the evaluation of the right hand side of an expression because the left hand side is enough to provide the answer.

For example: an expression like var result = 100 / number; will give you NaN when number = 0, but:

var result = number && 100 / number;

Will give you 0 instead of a NaN since 0 is falsy. In a boolean context false && anything is false, so there's no point in evaluating the right hand side. Similarly:

// supposed msg is a potentially empty string
var message = msg || "No message";

Will give you msg if the string msg is not empty (truthy) since true || anything is true. If msg is empty, it gives you "No message instead".

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • Oh. So, I guess the reason you use && instead of || is to prevent the program from moving forward with the check in the event of 0, but to keep it moving forward in the event of 1. Sweet. I totally understand, now. Sorry I didn't give the check out earlier. I was at work. :D – Coke Christopher Oct 03 '11 at 01:30
1

I think I understand what you mean, however, it's not.

var some_bool = (func_a() && func_b());

Now, when func_a returns false, func_b indeed doesn't get called. However, when it returns NaN (which is, just like 0, equal to false) it still returns false!

cutsoy
  • 10,127
  • 4
  • 40
  • 57