When 5 == 5.0 returns true, :atom == atom returns false. Why?

iex(1)> 5 == 5.0 
true

In the above code, the == operator checks if both sides of the value are same and it does not care about the data types. According to the Elixir guide, Atoms are constants whose name is their value. If it checks only the value, why does the below code return false?

iex(1)> :hello == "hello"
false

Thanks.

1 Like

Hello and welcome,

You mght find how operators works here

https://hexdocs.pm/elixir/master/operators.html

You have strict equality with === between numbers, but atoms will never be strings :slight_smile:

2 Likes

Notably strict equality === only matters when you are comparing integers and floats. In all other comparisons in Elixir, the comparison will be false if the data types are different.

3 Likes

:hello is an atom and internally represented as an integer. This is handled by the runtime. The exact value of the integer is not known at compile time. They are generated at runtime and stored in an atom table. Atoms are similar to static consts or enums in other languages. “hello” on the other side is a string.

3 Likes