Frank V
Frank V

Reputation: 25429

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

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?

Example from Kendo

Upvotes: 4

Views: 73

Answers (2)

TbWill4321
TbWill4321

Reputation: 8676

It's a trick for minifying:

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

It just does the same thing with less characters.

Upvotes: 5

tdc
tdc

Reputation: 5464

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.

Upvotes: 1

Related Questions