10

Why is it that when I do

(!true) ? 'false' : 'true'

it returns 'true'?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
JM at Work
  • 2,417
  • 7
  • 33
  • 46
  • 4
    FYI, if you're happen to want the string 'true' or 'false' from a variable, you can also type: `!var+''` (or `!!var+''` if you want it flipped) –  Apr 08 '11 at 06:14
  • 13
    What did you expect it to return? – Corneliu Apr 08 '11 at 08:45
  • 2
    Why hasn't this question contains an **accepted** answer? – Buhake Sindi May 09 '13 at 19:19
  • @Buhake, maybe JM at Work was confused by the opening parenthesis without a closing one and did not want to accept? – Amelse Etomer Jul 17 '13 at 20:10
  • @Sebastian Langer, Aaah! Well spotted, 2 years after posting the original answer! Thanks! :-) – Buhake Sindi Jul 17 '13 at 20:11
  • It is funny, that we now have the least and the highest scoring answers showing the same code. One of the answers should remove the parentheses at all to show some variety. :-) It would still be valid code. – Amelse Etomer Jul 17 '13 at 22:01
  • @djangofan Because the OP of the question hasn't been online at StackOverflow since the end of 2011. Odds are this question will remain unanswered. –  Aug 21 '13 at 15:22

8 Answers8

66

It simply means

if (!true) {
  return 'false';
} else {
  return 'true';
}

!true (not true) means false, so the else is returned.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
23

The syntax of A ? B : C means that if A is TRUE, then return the value B. Else return value C. Since A is FALSE, it returns the value C which happens to be true.

21

Because (!true) is false, and then the right side of the : is chosen.

hopper
  • 13,060
  • 7
  • 49
  • 53
Daniel
  • 27,718
  • 20
  • 89
  • 133
17

Because the above is equivalent to:

if (false) {
    return 'false';
} else {
    return 'true';
}

Though perhaps the confusion is coming from the difference between:

if (false) // which is false

And

if (false == false) // which is true
David Tang
  • 92,262
  • 30
  • 167
  • 149
7

This can be expanded to:

if(!true){
   return 'false';
} else {
   return 'true';
}
StKiller
  • 7,631
  • 10
  • 43
  • 56
1

if(!true) is equivalent to if(!true= true) which is equivalent to if(false=true) which is false. Therefore return (true) which is on the false side of the if statement.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
ndabenhle
  • 35
  • 1
1

The confusion lies here because of the use of string literals to represent boolean values. If you reverse the 'false' and 'true', it makes more sense:

(!true) ? 'true' : 'false'

Would return the string literal false, which is much different than a boolean value.

Your original statement (!true) ? 'false' : 'true' reads as

"If not true, then return the string literal true".

The statement I posted first reads as

"If not true, then return the string literal false".

Which, if you know the opposite (not) value of true is false, then it explains the logic illustrated.

Engineer2021
  • 3,288
  • 6
  • 29
  • 51
0

const test = true; // change this value to see the result change  
 
 if (!test) { // if test is NOT truthy
     console.log('false')
 } else { // if test === true
     console.log('true')
 }
// Since in the original question a ternary expression was used, the above code is equivalent to this:

!test ? console.log('false') : console.log('true');
Dženis H.
  • 7,284
  • 3
  • 25
  • 44