Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does !this mean in javascript?

As in this function, that toggles the 'completed' state of a todo item from true to false and back:

toggle: function() {
  this.save({
    completed: !this.get('completed') 
  });
}
like image 999
YPCrumble Avatar asked Jul 13 '26 15:07

YPCrumble


1 Answers

Assuming you know that ! means negation (converts to boolean and returns the opposite value), what you posted is the same as

!(this.get('completed')) 

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

Notice that the . comes before the ! on that table. That's why the ! is associated with the whole expression, the . gets associated with get() before the ! comes into play.

like image 129
Juan Mendes Avatar answered Jul 15 '26 04:07

Juan Mendes