简体   繁体   中英

What is the difference between == and === in Elixir?

I am starting out with Elixir. I read that Elixir has weak equality == and strict equality === operator.

Coming from the JavaScript background, == feels like a big mistake. Is it any different in Elixir? Am missing something? When should I use weak and strong operator respectively in Elixir?

So far I know, In JavaScript "1" == 1 yields true while in Elixir it yields false . It means at least type information is preserved. If this is so, then what is the use of weak equality?

According to the official documentation :

The difference between == and === is that the latter is more strict when comparing integers and floats:

 iex> 1 == 1.0 true iex> 1 === 1.0 false 

Otherwise they're pretty much the same:

→ iex
Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> "bro" == "bro"
true
iex(2)> "bro" ===  "bro"
true
iex(3)> 1 == 1
true
iex(4)> 1.0 == 1.0
true
iex(5)> 1 == 1.0
true
iex(6)> 1 === 1.0
false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM