1

I've been looking through some javascript code, and In various places, the developer uses !1, like so:

var somevar = !1;

from my understanding !1 is the same as false.

Is there any practical (whether it is to cover some edge case, performance, etc.) reason as to why they are using !1 instead of false?

kennypu
  • 5,950
  • 2
  • 22
  • 28
  • 2
    They probably drifted over from the C programming department. – Ted Hopp Oct 09 '13 at 02:53
  • 4
    You know what's shorter? `0` – sgbj Oct 09 '13 at 02:54
  • compiling with javascript minify's or just being fancy – Jay Harris Oct 09 '13 at 02:54
  • 1
    Some busy sites go to great lengths to save even a couple of bytes since it all adds up to bandwidth. I doubt there's much performance difference with modern Javascript engines. –  Oct 09 '13 at 02:55
  • 2
    @sbat `0` and `false` are two different things. – Joseph Oct 09 '13 at 02:57
  • so the most likely reason is just to save those few precious bytes? seems kind of overkill but I suppose. – kennypu Oct 09 '13 at 02:57
  • possible duplicate of [What does this exclamation mark mean in Javascript?](http://stackoverflow.com/questions/19129089/what-does-this-exclamation-mark-mean-in-javascript) – SheetJS Oct 09 '13 at 03:07

4 Answers4

4

It's a few less bytes. People are crazy about minifying these days!

Joseph
  • 12,678
  • 19
  • 76
  • 115
4

It's fewer bytes, true. However that's the kind of optimizations that should be left to the compressor itself and doesn't really belong in the source code as it makes the code less readable.

plalx
  • 42,889
  • 6
  • 74
  • 90
  • 1
    @Nirk It's not because worst practices exists that it makes this one acceptable. – plalx Oct 09 '13 at 03:17
  • 1
    @Nirk: Can't imagine why you'd think that `!function(){..}()` is a "worse sin" when it's a safer alternative. Also can't see how it's any less readable. "Not what I'm accustomed to" is not the same as "not readable". – user2736012 Oct 09 '13 at 03:36
  • @user2736012 Totally agree. – plalx Oct 09 '13 at 03:42
  • 2
    @Nirk "but somehow !0 is not as safe as false?" I guess you just proved that it's not by asking this question... `!0` is not `false`, it's `true`. – plalx Oct 09 '13 at 03:55
  • @Nirk: I made no comparison to `!0`. I'm saying `!function(){..}()` is a safer alternative to `(function(){})()`, so how can it be a worse sin, even granting that `!0` is not unsafe? Though as @plalx pointed out, your comment demonstrates the problem with that syntax. Good catch @plalx! :-) – user2736012 Oct 09 '13 at 04:10
  • Aren't we a little too significant about this? – dthree Oct 09 '13 at 04:28
2

No, there is no difference. You don't have any good reason to use it.

HA!!!!! It's slower!

var iterations = 100000000;

var start = new Date();
for (var i = 0; i < iterations; ++i) {
  var somevar = !1;
}
var end = new Date() - start;
console.log('SCORE 1: ' + end);

// Result: 2634ms


var start = new Date();
for (var i = 0; i < iterations; ++i) {
  var somevar = false;
}
var end = new Date() - start;
console.log('SCORE 2: ' + end);

// Result: 1979ms

I got multiple downvotes [among upvotes] because it's technically shorter [for smaller code], so I just had to do this for the performance do-gooders. Sorry.

Lastly, from Jeff Atwood, none other.

dthree
  • 19,847
  • 14
  • 77
  • 106
0

!1 is three characters shorter than false.

SheetJS
  • 22,470
  • 12
  • 65
  • 75
Soul Ec
  • 819
  • 7
  • 11