1

In the below code :

console.log( (false || "test") ? "first" : "second") );

The o/p of first part is test ( false || "test") , so how is my final o/p first ? What happens in general when the conditional operator can't evaluate an expression as True or False ?

Rohit P
  • 287
  • 3
  • 12

5 Answers5

3

|| is OR. So when a boolean is needed, (false || "test") is equivalent to "test". And "test" is coerced as true when a boolean is needed (0 wouldn't, "" wouldn't, but "0" would be seen as true).

As seen in the ECMAScript specification, an if converts the condition to a boolean using toBoolean :

enter image description here

Denys Séguret
  • 355,860
  • 83
  • 755
  • 726
  • So you mean this coercion happens only in case of "if" and not in case where I am assigning a value like (var x = false||"test") ? – Rohit P Oct 02 '12 at 13:30
  • It happens any time a boolean is needed. In your case x would be "test". This is frequently used for conditional initialization : `myObject = myObject || {}`; – Denys Séguret Oct 02 '12 at 13:32
  • See [this](http://stackoverflow.com/questions/11250449/can-you-add-a-condition-to-a-variable-declaration) – Denys Séguret Oct 02 '12 at 13:37
  • 1
    @RohitP - In JavaScript `||` is not a boolean operator, it always returns one of its operands (so it only returns an actual boolean if the operand being returned was an actual boolean). – nnnnnn Oct 02 '12 at 13:40
1

Every value in javascript, regardless of its type, can be coerced to a boolean value.

Values that coerce to false are called "falsey", and values that coerce to true are called "truthy".

Here's a fiddle demonstrating this coercion.

In this case:

(false || "test") ? "first" : "second")

(false || "test") is logically equivalent to ("test") since false || X is equivalent to X (this is called a disjunctive syllogism, if you're interested in logics).

Any non-empty string in javascript (including the string 'false', have fun with that bug) coerces to true, so the tertiary condition evaluates to true and logs 'first'.

jbabey
  • 44,525
  • 12
  • 67
  • 94
0

"What happens in general when the conditional operator can't evaluate an expression as True or False ?"

Every expression in JavaScript can be evaluated as true or false because every type is coercible to a boolean.

In your code, the "test" operand will be evaluated effectively as Boolean("test"), which is true.

The values that coerce to false, are:

  • false (obviously)
  • NaN
  • "" (an empty string)
  • 0
  • null
  • undefined

Everything else is considered true.

I Hate Lazy
  • 45,169
  • 12
  • 84
  • 76
0

The || operator returns the left hand side if the left hand side is true, otherwise it returns the right hand side.

Thus (false || "test") is "test".

"test" is a true value, so you get:

"test" ? "first" : "second"

The ternary operator returns the value before the : if the value before the ? is a true value (which is is).

What happens in general when the conditional operator can't evaluate an expression as True or False ?

The result of any expression can be evaluated as being a true or false value, so that will never happen.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
0

Because Javascript implicitly casts test to true. While converting strings to boolean, Javascript converts non-empty strings to true, and empty strings to false.

So, since the first operand of the || operator is false, false || "test" expression will return the second argument, which by then will be converted to true. So you will get first as your output.

MD Sayem Ahmed
  • 27,838
  • 25
  • 109
  • 176