-2

I came across this line while reading source code of an application.

$scope.editMode = ! $scope.editMode;

I wonder what does this is not the not the not equal to operator. I tried it in this jsfiddle the answer is correct but I still do not understand the logic is this somehow an equal to operator?

jsfiddlecode

  $scope.name = 'Superhero';
        $scope.hero = '123'    
        $scope.name = ! $scope.hero
Imi
  • 409
  • 1
  • 7
  • 20
  • 1
    It means assign to NOT it. It's just `x = not x`. So if `x = true;` then `x = !x;`, now `x == false` – Andrew Li Oct 28 '16 at 14:06
  • Do you know what the `!` operator does by itself? i.e. do you know what `!x` does? – JJJ Oct 28 '16 at 14:08
  • Also good the give a look on `coercion` – taguenizy Oct 28 '16 at 14:09
  • 4
    It's the same as `$scope.editMode = (!$scope.editMode)` if that's easier to read, it turns `$scope.editMode` into a boolean, false if it's truthy etc. In other words it's a "switch/flag", turning falsy values into true, and truthy values into false – adeneo Oct 28 '16 at 14:10

1 Answers1

3

! is used to reverse boolean value. For example:

!(true) = false and 
!(false) = true

In your example , reversion of $scope.editMode value has been assigned to itself.

Natiq
  • 426
  • 7
  • 15