Truthiness in code

Programming languages have different ideas about what is true and what is false.

Truthy languages

  • In Forth, any non-zero values are treated as true.

    Code example
    1 1 =  \ true, pushes -1 onto the stack
    1 2 =  \ false, pushes 0 onto the stack
    
    : TRUE? ( n -- )
        IF ." yes" ELSE ." no" THEN
    ;
    1  TRUE?  \ prints "yes"
    -1 TRUE?  \ prints "yes"
    0  TRUE?  \ prints "no"
    
  • In Racket, any value other than false (#f) is treated as true.

    Code example
    (if #f "true" "false")  ; "false"
    (if #t "true" "false")  ; "true"
    (if 0  "true" "false")  ; "true"
    
  • In Ruby, any value except false and nil is treated as true.

    Code example
    def assert!
      raise unless yield
    end
    
    def falsy_values
      assert! { not false }
      assert! { not nil }
    end
    
    def truthy_values
      assert! { true }
      assert! { 0 }
      assert! { '' }
      assert! { [] }
      assert! { {} }
    end
    
    falsy_values
    truthy_values
    

Falsy languages

  • In C, zero values, null values, and null pointers are falsy. Pointers to falsy values are treated as true (e.g. empty strings).

    Code example
    #include <assert.h>
    #include <stdlib.h>
    
    void falsy_values()
    {
        const int   i = 0;      assert(!i);
        const float f = 0.0;    assert(!f);
        const char  c = '\0';   assert(!c);
        const char *s = NULL;   assert(!s);
    }
    
    void truthy_values()
    {
        const int   i = 1;      assert(i);
        const float f = 1.0;    assert(f);
        const char  c = '0';    assert(c);
        const char *s = "";     assert(s);
    }
    
    int main()
    {
      falsy_values();
      truthy_values();
      return 0;
    }
    
  • In JavaScript, zero values, null values, and empty strings are falsy. However, empty arrays and empty objects are treated as true.

    Code example
    function falsy_values() {
      if (false)     { throw '!' }
      if (null)      { throw '!' }
      if (undefined) { throw '!' }
      if (NaN)       { throw '!' }
      if (0)         { throw '!' }
      if ('')        { throw '!' }
    }
    
    function truthy_values() {
      if (!true)     { throw '!' }
      if (!1)        { throw '!' }
      if (!'0')      { throw '!' }
      if (![])       { throw '!' }
      if (!{})       { throw '!' }
    }
    
    falsy_values()
    truthy_values()
    
  • In Python, blank/empty values are generally falsy: zero values, empty strings, empty lists, and so on.

    Code example
    def falsy_values():
        assert not False
        assert not None
        assert not 0
        assert not ''
        assert not ()
        assert not []
        assert not set()
        assert not {}
    
    def truthy_values():
        assert True
        assert 1
        assert '0'
        assert (None,)
        assert [None]
        assert {None}
        assert {None: None}
    
    falsy_values()
    truthy_values()