Home Elixir boolean operators
Post
Cancel

Elixir boolean operators

There’s subtle difference between Elixir boolean operators. Namely between or and ||, and and &&, not and !.

These operators expect true or false as their first argument:

1
2
3
a or b	# true if a is true; otherwise b
a and b # false if a is false; otherwise b
not a	# false if a is true; otherwise b

These operators take arguments of any type. Any value apart from nil or false is interpreted as true:

1
2
3
a || b	# a if a is truthy; otherwise b
a && b	# b if a is truthy; otherwise a
!a	# false if a is truthy; otherwise true

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
iex(1)> 1 or 2
** (BadBooleanError) expected a boolean on left-side of "or", got: 1

iex(1)> 1 || 2
1

iex(1)> true or false
true

iex(2)> "foo" and "bar"
** (BadBooleanError) expected a boolean on left-side of "and", got: "foo"

iex(2)> "foo" && "bar"
"bar"

iex(3)> not "foo"
** (ArgumentError) argument error
    :erlang.not("foo")

iex(3)> !"foo"
false
This post is licensed under CC BY 4.0 by the author.