Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: what is the meaning of nil

Tags:

null

elixir

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

like image 376
Liz Parody Avatar asked Sep 27 '17 20:09

Liz Parody


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.

like image 151
helios35 Avatar answered Nov 07 '22 07:11

helios35