Skip to content

Instantly share code, notes, and snippets.

@jmichalicek
Last active September 1, 2017 02:10
Show Gist options
  • Save jmichalicek/8e97174dddeab303d6b3d390d3413f0a to your computer and use it in GitHub Desktop.
Save jmichalicek/8e97174dddeab303d6b3d390d3413f0a to your computer and use it in GitHub Desktop.
Simple truthy vs falsey examples in a few languages

Python

Python 3.6.1 (default, Apr 12 2017, 14:04:19)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if 0:
...     print("0 is truthy")
... else:
...     print("0 is falsey")
...
0 is falsey
>>> if "0":
...     print('"0" is truthy')
... else:
...     print('"0" is falsey')
...
"0" is truthy
>>>

Ruby

ruby 2.1.5p273 (2014-11-13) [x86_64-linux-gnu]
justin@Blargh:~$ irb
irb(main):001:0> if 0
irb(main):002:1> puts "0 is truthy"
irb(main):003:1> else
irb(main):004:1* puts "0 is falsey"
irb(main):005:1> end
0 is truthy
=> nil
irb(main):006:0> if "0"
irb(main):007:1> puts '"0" is truthy'
irb(main):008:1> else
irb(main):009:1* puts '"0" is falsey'
irb(main):010:1> end
(irb):10: warning: string literal in condition
"0" is truthy
=> nil
irb(main):011:0> if false
irb(main):012:1> puts "false is truthy"
irb(main):013:1> else
irb(main):014:1* puts "false is falsey"
irb(main):015:1> end
false is falsey
=> nil

Javascript:

if(true) {
    console.log("true is truthy");
} else {
    console.log("true is falsey");
}
VM227:2 true is truthy
undefined
if (0) {
  console.log("0 is truthy");
} else {
  console.log("0 is falsey");
}
VM313:4 0 is falsey
undefined
if ("0") {
    console.log('"0" is truthy');
} else {
    console.log('"0" is falsey');
}
VM386:2 "0" is truthy

Elixir

justin@Blargh:~$ iex
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> if false, do: IO.puts("false is truthy")
nil
iex(2)> if true, do: IO.puts("true is truthy")
true is truthy
:ok
iex(3)> if 0, do: IO.puts("0 is truthy")
0 is truthy
:ok
iex(4)> if "0", do: IO.puts('"0" is truthy')
"0" is truthy
:ok
iex(5)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment