0

In Javascript, 2 || 3 returns 2. I understand that the OR operator here basically works like 2 ? 2 : 3, and that this can still work like a boolean in conditionals, like if (2 || 3){...} evaluates to true. So my current understanding of boolean operators in Javascript is that they do not actually return boolean values, but that they can be used in the same way that booleans can in conditionals, and can yield other useful properties like how a || b is a shorthand for a ? a : b. So my questions are:

1) Am I correct in saying "boolean operators in Javascript do not return boolean values"?

2) If so, is the reason they do not return boolean values that they can have other useful properties like the shorthand one above?

Thanks!

gkeenley
  • 6,088
  • 8
  • 54
  • 129
  • `||` is a *logical OR operation*. Not restrictive to *boolean*. – Taplar Jun 05 '19 at 15:35
  • In javascript, there exists `Coercion`, `falsy` and `truthy` values. – m1k1o Jun 05 '19 at 15:35
  • _Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the `&&` and `||` operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value._ ([ref.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical_operators)) – hindmost Jun 05 '19 at 15:37
  • There is no such thing as a "boolean operator" in JavaScript. So your _current understanding_ is based upon a false assumption in the first place. – Randy Casburn Jun 05 '19 at 15:41

0 Answers0