Truthiness in code

C

#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;
}

JavaScript

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()

Python

‘Empty’ values are generally falsy in Python.

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()

Racket

Only #f is false.

Ruby

Only false and nil are falsy in Ruby.

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