Truthy or Falsy?

Published by Helen Yu in

A "truthy" value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they're defined as falsy.

All falsy values are as follows:

  • false
  • null
  • 0
  • ""

Create a function that takes an argument of any data type and returns 1 if it's truthy and 0 if it's falsy.

Examples

isTruthy(0) ➞ 0

isTruthy(false) ➞ 0

isTruthy("") ➞ 0

isTruthy("false") ➞ 1

Notes

  • To people with experience in JavaScript: in PHP, an empty array is false (while in JavaScript, it is true).
  • Don't forget to return the result.
  • If you get stuck on a challenge, find help in the Resources tab.
  • If you're really stuck, unlock solutions in the Solutions tab.
Watch a quick demo on how Edabit works.