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
  • 337
  • 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
  • 372,613
  • 87
  • 782
  • 758
  • 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
  • 45,965
  • 12
  • 71
  • 94
  • Thanks! So then why (false||"test") doesn't evaluate to true ? – Rohit P Oct 02 '12 at 13:25
  • `(false||"test")` does evaluate to true. what makes you think it doesn't? – jbabey Oct 02 '12 at 13:28
  • coz when I assign that to a variable like var x = false||"test" , I get "test" as o/p. – Rohit P Oct 02 '12 at 13:32
  • correct, and if you were to put that `"test"` into a conditional statement, it would be coerced to a boolean: `true`. – jbabey Oct 02 '12 at 13:34
  • 1
    _"have fun with that bug"_ - It's not a bug in the language, though people who don't understand this point will write JS code that doesn't work. – nnnnnn Oct 02 '12 at 13:38
  • 1
    @nnnnnn absolutely - i was referring to the bug in application code caused by a misunderstanding of the concept, not a bug in the language. – jbabey Oct 02 '12 at 13:42
  • @RohitP You can accept only one answer (and only you can choose) but you can upvote all the answers you like. – Denys Séguret Oct 02 '12 at 13:48
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
  • 47,415
  • 13
  • 86
  • 77
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
  • 914,110
  • 126
  • 1,211
  • 1,335
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
  • 28,628
  • 27
  • 111
  • 178