-4

Is there any difference I have to take care by using one of these comparisons or are they completly identical:

return !!(foo == 'bar')

return (foo == 'bar') ? true : false;

if (foo == 'bar') return true;
else return false;
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • 4
    You missed the best option: `return foo === 'bar'` – elclanrs Jun 12 '16 at 19:56
  • Also: `return foo == "bar" && true || false`, `return Boolean(foo == "bar");`, `return [false, true][Number(foo == "bar")];` or anything else you might want to do. – Bergi Jun 12 '16 at 20:01
  • Possible duplicate of [What is the !! (not not) operator in JavaScript?](http://stackoverflow.com/q/784929/1529630) and [Question Mark in JavaScript](http://stackoverflow.com/q/1771786/1529630) – Oriol Jun 12 '16 at 20:05

1 Answers1

-1

The better one (not included in the list) is

return foo === 'bar';

All expressions return the same result, but because of == or === operators return a boolean value you can simplify the expression and have a more readable code.

Dario
  • 3,905
  • 2
  • 13
  • 27