Elixir: what is the meaning of nil

Viewed 9585

I'm learning elixir and I was wondering if nil is the same as null in JavaScript, if not, what is the meaning?

1 Answers

They are similar, both are often used to denote the absence of a value. In JavaScript, !!null evaluates to false, while in Elixir, !!nil evaluates to false. Just like JavaScripts null, you can use nil as a "falsy" value in a boolean context, for instance:

if nil do
  IO.puts "foo"
else
  IO.puts "bar"
end

will print "bar"

I can't think of many differences between the two, other than perhaps a few corner cases. For instance, null > 0 evaluates to false in JavaScript, while nil > 0 evaluates to true in Elixir.

Related