-1

What are the differences between using condition, coercion and Boolean conversion in checking a number type for greater than zero (0)?

Each will give the same output

var zero = 0;
var three = 3;

// condition
zero > 0 //false
three > 0 //true
null > 0 //false

// coercion
zero ? true : false; //false
three ? true : false; //true
null ? true : false; //false

// Boolean conversion
Boolean(zero); //false
Boolean(three); //true
Boolean(null); //false

What is the difference between these operations? (In terms of speed/performance, principles, practices, etc.)

Alex Pappas
  • 2,377
  • 3
  • 24
  • 48
  • why conditional operator for already boolean values? btw, zero is falsy, and any negative number is truthy. – Nina Scholz Jan 17 '19 at 09:32
  • 1
    `zero > 0` is a boolean while `zero ? ...` is a [truthy or falsy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) value. By the way `zero > 0` <=> `zero > 0 ? true : false` – Cinn Jan 17 '19 at 09:35
  • The condition expression is the only one that works if checking negative numbers, so would make more sense to use that one I guess ... – Nsevens Jan 17 '19 at 09:35
  • See this : http://adripofjavascript.com/blog/drips/the-difference-between-boolean-objects-and-boolean-primitives-in-javascript.html – Alexandre Elshobokshy Jan 17 '19 at 09:36
  • @NinaScholz Thanks for pointing that out. I updated it now. – Alex Pappas Jan 17 '19 at 09:44
  • @Cinn, Thanks for pointing the first two examples out. I updated it now – Alex Pappas Jan 17 '19 at 09:46
  • Btw, Am I using the right term for "coercion"? Is it different from "truthy"? – Alex Pappas Jan 17 '19 at 09:53
  • 1
    @ColdCerberus you used it well, and it is different from truthy, a good answer about this: https://stackoverflow.com/a/19915864/3936644 – Cinn Jan 17 '19 at 10:02
  • first condition of ternary operater needs to be a boolean expression. But above in your case you are doing assignment not evaluating a boolean expression. So in this case whatever value is in isGreaterThanZero will be used as boolean expression on the basis of value of this variable ternary operator will be evaluated. –  Jan 17 '19 at 10:05

3 Answers3

1

Both condition and coercion use js boolean logic (you can check ECMA-262 standard for more details), while Boolean constructor, in fact, implements this logic itself.

Therefore speed/perfomance -> Boolean() wins. Principles/practice/etc. -> depending on the codestyle you follow

bohkoval
  • 365
  • 1
  • 9
1

In this case, Boolean conversion isGreaterThanZero= Boolean(zero) is best in terms of speed/performance because there is no logical condition (if..else) behind this.

And remaining methods condition and coercion conversion is a bit slow due to it's logical condition (if..else).

isGreaterThanZero= zero > 0
isGreaterThanZero= zero ? true : false
AtulParmar
  • 4,358
  • 1
  • 24
  • 45
1

The condition and coercion you've mentioned are both types of implicit coercion.

Implicit coercion refers to type conversions that aren't as readable or developer friendly. And from what I've learned, a lot of the hatred towards implicit coercion comes from the fact that it doesn't specify exactly what is being converted and can therefore create unexpected or unintended results.

Boolean conversion is explicit coercion, meaning that it is more readable for developers and specifies what's being converted (unlike what is implied).

I don't have solid answers on speed/performance and principles in regards to your questions, but if you're concerned about readability (for future reference or another developer) that Boolean conversion is the way to go.

underthecode
  • 116
  • 1
  • 5