“Truthy” and “Falsy”

The idea that an if statement expects a “yes” or “no” question as its argument is only partially correct. What would happen if you use something other than a “yes” or “no” question, as in Listing 7.11? Try it out in the console.

Listing 7.11 When if Meets a String

> if ('true') {    console.log('"true" is true');  }  "true" is true> if ('false') {    console.log('"false" is true(?)');  }  "false" is true(?)

How did 'false' become true? Many programming languages have a notion of “truthiness,” which means a value in any data type is either “truthy” or “falsy.” The truthiness of a value is determined by what is returned when that value is coerced to a Boolean, either true or false. Each programming ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.