41

If you go to a google result page, and run rwt.toString(), you'll see that the return call for this function is:

return !0;

I can't think of any reason why this wouldn't always be true. Is this just a shorthand for true, or is there more going on here?

Charles
  • 50,943
  • 13
  • 104
  • 142
Darth Egregious
  • 18,184
  • 3
  • 32
  • 54

1 Answers1

48

It is always true, but it takes 2 bytes to download (!0 is 2 characters) instead of 4 bytes to download the boolean value true.

Most Javascript minifiers will convert true to !0 and false to !1. You can see an example of this by typing var y = true; with Simple optimizations on Google's Closure Compiler: http://closure-compiler.appspot.com/home

Paul
  • 139,544
  • 27
  • 275
  • 264
  • 2
    however it only takes 1 byte to download 1. return 1; would have worked just as well, no? being non-zero, and we all know you don't test for true, you test for not false. – stu Jan 05 '12 at 21:34
  • This is interesting. Do most javascript implementations optimize !0 and !1 to true and false without calculating? – Kekoa Jan 05 '12 at 21:41
  • 8
    @stu `return 1;` returns a `number` type. If someone were to use the return value with the `===` or `!==` operators, they'll get expected results from `!0`, but not from `1`. – Paul Jun 22 '12 at 16:58
  • @Kekoa What stu is saying (correct me if I'm wrong, here, stu) is that a minifier will optimize `true` to `!0` and `false` to `!1` to save bandwidth. These expressions get calculated inline by the browser's JavaScript implementation to return boolean values of `true` and `false`, respectively, which is why they pass the strict-equality (`===`) test. – Speaker-to-Animals Nov 16 '12 at 13:43