0

I'm new to learning javascript. I've followed a few basic tutorials. I'm trying to understand a script that I found. This script, in several places has something like the following:

var USE_CANVAS = !0;
if ("trydom" == window.location.hash || "#trydom" == window.location.hash) USE_CANVAS = !1;

I have no idea what this means? When I evaluate for this variable by simply typing it into the console, it returns 'true'. I can't find any references to this style online. I should mention that the script is dependent on underscore.js and d3.js, however i personally haven't found anything in these two libraries that looks anything like the above.

Another line that I also cannot explain has to do with the use of "question" and "colon" like so:

b.select(".y.axis").call(j)) : "pop" === a ? (c = "rMVMP", j.scale(C).tickV…

You can ignore the references to select,call and scale, since these are all d3js functions. I'm only pasting the second line because I'm not sure whether this is 'javascript' or a completely different language. The file however is named 'chart.js'.

Regards, Richard.

Algorini
  • 824
  • 1
  • 12
  • 19
  • I am curious to see what others say. 0 computes as false 1 as true And ! is just a negation, so !0 becomes true and !1 becomes false. Now you tell me why... ? – htatche Jul 06 '15 at 21:16
  • 1
    it means boolean false. A usual case for this is a minifier will convert false to !1 – Rooster Jul 06 '15 at 21:16
  • It is usually used to minimize the file size. This means faster page loads etc. In a big file, this can make a significant difference in file size. – Oliver Jul 06 '15 at 21:17
  • 2
    [Javascript language reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference) and [?:](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). Happy reading – Amit Jul 06 '15 at 21:18
  • 1
    ! is the boolean not operator. this is a duplicate of http://stackoverflow.com/questions/11550681/what-does-1-and-0-mean-in-javascript. Incidentally ! is the boolean not operator in so many other languages it may be condisered a programming idiom in the context of logical operators. If ones want to set a variable to true|false it is a better practice to do it explicitly, as in a = true or b = false and not rely on convention that 0 evaluates to false and 1 evaluates to true ina boolean context. –  Jul 06 '15 at 21:21
  • 1
    Regarding !0 and so on, as other pointed out it's a minification construct, and it's a terrible decision to do that in modern code bases. Code should be readable and maintainable. Minification is a build step. – Amit Jul 06 '15 at 21:22

2 Answers2

4

In Javascript, 1 is "truthy", meaning that 1 can represent a boolean true.

That means that 1 can essentially mean true. Likewise, 0 is falsy, meaning false.

So if something is !1, it's "not true" which means it's false.

var USE_CANVAS = !0;

is essentially the same as:

var USE_CANVAS = true;

but takes less space and is usually how minified JS looks.

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59
  • @JunedAhsan no, that is not true in JavaScript. `0`, `NaN`, `""`, `false`, `null`, and `undefined` are also interpreted as `false` in a boolean context. – Pointy Jul 06 '15 at 21:21
  • 2
    Good point about the minified JS. It is helpful to know *why* this code would ever exist. – EJK Jul 06 '15 at 21:21
  • 1
    this code comes from the nytimes page: http://www.nytimes.com/interactive/2012/05/17/business/dealbook/how-the-facebook-offering-compares.html?_r=0 – Algorini Jul 06 '15 at 21:26
0

The expression !1 evaluates to false. It's not clear why anyone would write code like that.

The ? : operator stems from the legacy of C. It's the "ternary" operator, and it's sort-of a shorthand (in expression form) for an if ... else statement.

Thus

test ? whenTrue : whenFalse

will evaluate to whenTrue when test is true (or "truthy"), and whenFalse when it isn't.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • maybe when someone wants to compare it with another integer representing a boolean value with a type value check too. – Juned Ahsan Jul 06 '15 at 21:18
  • 1
    Yes it could be for any reason like that. – Pointy Jul 06 '15 at 21:19
  • Appears very "golfy", but is strange to see without other minification – Brennan Jul 06 '15 at 21:21
  • I'll mark this as the right answer because you answered both my questions! You can find where this code came from on the following page : New york times , visualization of the facebook ipo: http://www.nytimes.com/interactive/2012/05/17/business/dealbook/how-the-facebook-offering-compares.html?_r=0 – Algorini Jul 06 '15 at 21:23