if-statements, continued

Learn what truthy and falsey mean in JavaScript. Learn how to make if-statements more powerful.

“Truthy” and “Falsey”

If-statements don’t work only with booleans. We can use any variable type inside the if-statement parentheses.

If we don’t use true or false variables, JavaScript will forcibly coerce whatever we put inside the parentheses to true or false. It’ll then decide whether to run the code or not.

A value that coerces to true is referred to as “truthy”. One that coerces to false is “falsey”.

So how do we determine what a value will coerce to? It’s actually pretty simple. The following values are “falsey” and will coerce to false, meaning the code in the if-statement won’t run.

  • false
  • null
  • undefined
  • '' or "" (empty, 0-length string)
  • 0 (the number zero)
  • NaN

All other values are truthy. This means that all numbers except 0 and NaN and all strings that are not empty are truthy.

Try setting itemToTest equal to different values in the code block below. Test them out.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy