1

I am writing a code using WebStorm IDE, and I wrote an

if statement

in a method like this:

createEvent():boolean {
  return this.http.post('/event', { params: {name , location} }).map(res => res.json()).map(res => {

    if(event && event.success){
      return true;
    }

    return false;
  });
}

Then the IDE suggest I rewrite (simplify) the if statement this way:

createEvent():boolean {
  return this.http.post('/event', { params: {name , location} }).map(res => res.json()).map(res => {
    return !!(event && event.success);
  });
}

I find it confusing while the system uses a double NOT sign.

Please I need some explanation.

LazyOne
  • 148,457
  • 42
  • 363
  • 369
Lekens
  • 1,489
  • 13
  • 28
  • In normal JS, this is probably used for type conversion or for making the code smaller. – D. Pardal Apr 27 '18 at 11:13
  • Really you should just do `return event && event.success`. That already produces a *truthy* or *falsey* value. There's virtually never any place to use `if ... return true else return false`. – deceze Apr 27 '18 at 11:13
  • please see https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript – lena Apr 27 '18 at 11:21

0 Answers0