8

This expression ' ' == true returns false, which means ' ' is falsy,

However if(' ') { console.log(' true ') } else { console.log(' false ') } , gets the result true.

Now I am confused, whether the string of whitespace is truthy or falsy?

Rovanion
  • 3,842
  • 3
  • 25
  • 44
Johnny Chen
  • 1,925
  • 1
  • 15
  • 26
  • 1
    http://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-when-tested-by-if-it-is-not-fals – epascarello Nov 14 '15 at 05:27
  • *truthy* and *falsy* refer to whether the value evaluates to *true* or *false* when used **alone** in a logical test, like `if (' ') { console.log('truthy'); } else { console.log('falsy'); }` – André Chalella Nov 14 '15 at 05:30
  • Using *loose comparison* to *compare* to a Boolean is very different from *converting* a value to a Boolean. – Felix Kling Nov 14 '15 at 06:17

5 Answers5

15

The string ' ' is a "truthy" value.

Here is a list of the "falsy" values:

false
null
undefined
0
NaN
''

You have to understand that "truthy" is not the same thing as true. A value can be "truthy" without being true. For example, if we did 5 == true, we would get false, even though 5 is a "truthy" value.

In general, pretty much every value is "truthy" (excluding the ones mentioned above). But, the easiest way to check if something is "truthy"/"falsy" is by doing something like this:

var value = valueToTest;

if (value) {
  console.log('Truthy!');
} else {
  console.log('Falsy!');
}
Saad
  • 32,434
  • 19
  • 55
  • 98
  • 1
    It's worth pointing out that `5 == true` ends up comparing numbers. If loose comparison is used to compare, Boolean values are converted to numbers. That's why `' ' == false` is `true`. – Felix Kling Nov 14 '15 at 06:20
  • (5== true) Is true – beginner Nov 06 '16 at 15:10
  • @beginner, not sure what you mean? If you evaluate `5 == true` in the console, you will get `false`. – Saad Nov 06 '16 at 20:07
  • Sorry i was into PHP , and In php `5== true` is `true` try `echo (5== true)` you will get 1, And i thought , it will be same in javascript , but strangelly it is different , (I just read Felix Kling comment) – beginner Nov 07 '16 at 05:26
  • Actually, the easiest way to test if something is truthy is `!!val`. –  Nov 29 '16 at 12:31
11

Is a string of whitespace truthy or falsy?

It's truthy, as others indicated. However, your comparsion

' ' == true

is not checking whether ' ' is truthy. It's comparing two values, which is a different thing and uses specific comparison rules. The JS spec is that any == comparison with a boolean first coerces the boolean to a number:

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

So your code is equivalent to

' ' == Number(true)

which is the same as

' ' == 1

Next, to compare a string and a number, JS converts the string to a number. Again, quoting the spec:

If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

So we now have

Number(' ') == 1

Number(' ') is 0. The spec says:

The MV of StringNumericLiteral ::: StrWhiteSpace is 0.

So this becomes

0 == 1

which, as you found, is false. See http://ecma262-5.com/ELS5_HTML.htm#Section_11.9.3 for details.

As other answers have explained not being equal (==) to true is not the same as being falsy. Falsy is a different concept, which roughly means the value to which an expression is coerced when a boolean is required (such as the condition of an if statement) is false.

David Souther
  • 7,863
  • 2
  • 33
  • 51
-1

in first case expression you are comparing ' ' whitespace string with bool value true and that will surely result in false as ' ' is not equal to true.

in second case expression you are simply writing ' ' whitespace in conditional check in if statement. according to the execution rule of if statement if value is present that it will evaluate to true. and whitespace character is not null so it will execute code block for true statement.

Hope this will help.

following value will evaluated to false if directly placed inside if statement test.

false
null
nil
undefined
NaN
''
0

values other than above will evaluate to true

  • Not exactly. An empty string `''` is also not null, yet it will cause the `if` statement to not be evaluated, since it's falsy. –  Nov 14 '15 at 06:10
  • yes empty string is falsy that's why i've included it in above snippet. this all will evaluate to false and if statement will not be evaluated. – Rajendrasinh Parmar Nov 14 '15 at 06:13
-3

From your code ' ' == true you are comparing string with a boolean so its obvious those 2 things are totally different and hence you get return as false

In second case i.e. if(' ') you are checking whether you have an object or a null. Here ' ' is a valid non-null object hence you get result as true.

vijayP
  • 11,262
  • 5
  • 21
  • 37
  • `' '` is not an object. It is a string. –  Nov 14 '15 at 06:07
  • 1
    You say *from your code `' ' == true` you are comparing string with a boolean so its obvious those 2 things are totally different and hence you get return as false* Yet `' ' == false` is also comparing a string with a boolean, which by your logic should be "completely different", yet returns true, so at minimum your answer is not complete. –  Nov 14 '15 at 06:30
-3

' ' is equal to 32 in ASCII. so , ' ' == true means 32 == true, if(' ')" means if (32).

wesley
  • 1