21

I have just come across a function in javascript which has return !1

I was just wondering what this actually meant?

Why would you return !1 or return !0

Could someone please explain what it means please?

Here is the function that I came across:

function convertStringToBoolean(a) {
    typeof a == "string" && (a = a.toLowerCase());
    switch (a) {
    case "1":
    case "true":
    case "yes":
    case "y":
    case 1:
    case !0:
        return !0;
    default:
        return !1
    }
}

Thanks In advance!

DarkMantis
  • 1,510
  • 5
  • 19
  • 40
  • 12
    You should only ever see this in minified code. If developers are actually coding this way, they should be taken out back and shot. – Dexygen Feb 19 '15 at 19:29
  • Possible duplicate of the broader [What is an exclamation point in JavaScript?](http://stackoverflow.com/q/8012003/1529630). – Oriol Apr 21 '16 at 10:37
  • Have a look at [Is there any point of using “return !0” in javascript?](http://stackoverflow.com/q/8750104/1048572) – Bergi Aug 23 '16 at 20:24

3 Answers3

23

In immediate response to your question:

  • return !1 is equivalent to return false
  • return !0 is equivalent to return true

In the specification - 11.4.9 Logical NOT Operator - it states that when you place an exclamation mark ! in front, the result is evaluated as Boolean and the opposite is returned.

Example:

var a = 1, b = 0;
var c = a || b;
alert("c = " + c + " " + typeof c); // here typeof c will be "number"

a = !0, b = !1;
c = a || b;
alert("c = " + c + " " + typeof c); // here typeof c will be "boolean"

I mostly see this in a code passed through Google's JS optimiser. I think it is mostly done to achieve shortness of the code.

It is often used when a strictly Boolean result is needed - you may see something like !!(expression). Search in jQuery, for example.

Robbie JW
  • 729
  • 1
  • 9
  • 22
Bakudan
  • 19,134
  • 9
  • 53
  • 73
  • Because the file was compressed I put it through code beautifier (http://jsbeautifier.org/) and it replaced true/false with !0 and !1; I'm not sure why though. I just checked the original but damn it was difficult to find :P Thanks again all! – DarkMantis Nov 24 '11 at 10:42
  • Wouldn't that actually create a hit to code performance? A negligible hit perhaps but the same could be said of compressing 4-5 bytes to two to reduce download time of the JS. – Erik Reppen Oct 02 '13 at 18:58
  • It's completely insignificant performance loss. Check out http://jsperf.com/negated-0-vs-false and you'll see that even the slowest browser can evaluate either one over 170 million times per second. – user24601 Jan 09 '14 at 20:26
  • 2
    But why use !1 instead of 0? 0 Would be even shorter wouldn't it? – Mike Bovenlander Apr 10 '15 at 10:03
  • 6
    @Mike Yes, it would But then it will be a number, and not a boolean. – Bakudan Apr 10 '15 at 15:12
11

This seems to be a particularly silly way of returning true or false

spender
  • 117,338
  • 33
  • 229
  • 351
-3

Here the code is verifying :

  • to return nothing or do nothing on these cases : "case 1", "case true", "case yes", "case y", "Case 1"
  • and when the case is : "case !0" return "true"
  • when none of the above cases are been satisfied by default it returns "false"
Poelinca Dorin
  • 9,577
  • 2
  • 39
  • 43
  • 2
    This is wrong. This function returns true for all the cases in your first bullet in addition to !0, false otherwise. Not to mention this answer does not address what the user is asking. – trigoman Aug 14 '14 at 19:07