In Elixir, there are three types of equality operators:
=
: This operator is used for pattern matching.==
: This operator compares if the LHS value equals the RHS value.===
: This operator checks whether both sides have the same data types and then compares if LHS value equals RHS value.Operator | Name | Expression | Return |
|
|
| 1 |
| Equal to |
| True |
| Strictly equal to |
| False |
There are two types of inequality operators:
!=
: This operator is used to compare if the LHS value does not equal the RHS value.
!==
: This operator checks if both data types are not the same and then compares if the LHS value does not equal the RHS value.
Operator | Name | Expression | Return |
| Unequal to |
| True |
| Strictly unequal to |
| True |
Let’s look at the example below:
# = OperartorIO.puts "= Operator: Pattern Matching: 1 = 1"#IO.puts "Pattern Matching:"IO.puts 1 = 1IO.puts "\n"# == OperatorIO.puts "== Operator"IO.puts "Same value and same data type: 2 == 2"IO.puts 2 == 2IO.puts "\n"IO.puts "Same value and different data type: 2 == 2.0"IO.puts 2 == 2.0IO.puts "\n"IO.puts "Differnet value and same data type: 2 == 3"IO.puts 2 == 3IO.puts "\n"IO.puts "Differnet value and differnet data type: 2 == 3.0"IO.puts 2 == 3.0IO.puts "\n"# === OperatorIO.puts "=== Operator"IO.puts "Same value and same data type: 2 === 2"IO.puts 2 === 2IO.puts "\n"IO.puts "Same value and different data type: 2 === 2.0"IO.puts 2 === 2.0IO.puts "\n"IO.puts "Differnet value and same data type: 2 === 3"IO.puts 2 === 3IO.puts "\n"IO.puts "Differnet value and differnet data type: 2 === 3.0"IO.puts 2 === 3.0IO.puts "\n"# != OperatorIO.puts "!= Operator"IO.puts "Same value and different data type: 1 != 1.0"IO.puts 1 != 1.0IO.puts "\n"IO.puts "Same value and same data type: 1 != 1"IO.puts 1 != 1IO.puts "\n"IO.puts "Different value and same data type: 1 != 2"IO.puts 1 != 2IO.puts "\n"IO.puts "Different value and different data type: 1 != 2.0"IO.puts 1 != 2.0IO.puts "\n"# !== OperatorIO.puts "!== Operator"IO.puts "Same value and different data type: 1 !== 1.0"IO.puts 1 !== 1.0IO.puts "\n"IO.puts "Same value and same data type: 1 !== 1"IO.puts 1 !== 1IO.puts "\n"IO.puts "Different value and same data type: 1 !== 2"IO.puts 1 !== 2IO.puts "\n"IO.puts "Different value and different data type: 1 !== 2.0"IO.puts 1 !== 2.0
Free Resources