Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle function with ! in jquery

Tags:

jquery

I'm reading someone's source code for their backbone app and trying to figure out this toggle function. I understand the basic of toggle is that it just switches back and forth between a setting. In the example below, it sets the favorite on a model (indicating if it's a favorite or not, I assume). I also understand the use of ! (meaning not). However, I don't get the logic of how this function works.

 toggle: function() {
      this.save({favorite: !this.get("favorite")});
    },

In particular, I don't get the

!this.get("favorite")

Isn't this.get("favorite") returning a string, namely "favorite"? or is this.get("favorite") returning the attribute which is a boolean?

Can someone kindly explain in plain english how the logic of this function works each time it would be called, toggling between the two states.

like image 629
Leahcim Avatar asked Jun 28 '26 00:06

Leahcim


1 Answers

! is a negation.

The function probably returns a boolean, which result then is inverted.

So in this case it would toggle favorite between true and false

like image 97
worenga Avatar answered Jun 29 '26 15:06

worenga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!