jQuery's usage of return !1 or return !0

354
December 11, 2016, at 00:01 AM

I'm seeing quite a bit of usage of !0 and !1 within the source code for jQuery and Telerks' JS. I've most seen it in return statements which may have some bearing on it's usage.

What is the purpose? Is this some sort of optimization?

Answer 1

It's a trick for minifying:

!0 === true
!1 === false

It just does the same thing with less characters.

Answer 2

I guess technically it is an optimization, but not one I encourage you to do by hand. a minifier will do this for you.

It's simply to return a boolean value.

!0 == true // because 1 is true, 0 is false, so NOT 0 is true.
!1 == false // same as above logic but flipped.
Rent Charter Buses Company
READ ALSO
BackboneJs Collections and Rest API calls [closed]

BackboneJs Collections and Rest API calls [closed]

I am currently working on a blog in backbone - now previously to this any backbone app I created has been fairly small so i should fetch all my collection on the initialisation on the app, store it in collections and pass them collections through to my view for usage

288
Is jquery .bind() deprecated?

Is jquery .bind() deprecated?

Is jquery bind() deprecated or it can be used safely?

549
hyperlink to specific tab on another page of same site

hyperlink to specific tab on another page of same site

I have a set of tabs here: http://thetalbotcuckfieldco

283
jQuery scrollTop and focus on element

jQuery scrollTop and focus on element

I have the following scrollTop function:

432