Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

elixir

Differences between the various equal operators in Elixir

Azeem Ashraf

Equality operator

In Elixir, there are three types of equality operators:

  1. =: This operator is used for pattern matching.
  2. ==: This operator compares if the LHS value equals the RHS value.
  3. ===: This operator checks whether both sides have the same data types and then compares if LHS value equals RHS value.

Operator

Name

Expression

Return

=

Match

1 = 1

1

==

Equal to

2 == 2.0

True

===

Strictly equal to

5 === 5.0

False

Inequality operator

There are two types of inequality operators:

  1. !=: This operator is used to compare if the LHS value does not equal the RHS value.

  2. !==: ​​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

2 != 3

True

!==

Strictly unequal to

2 !== 3

True

Example

Let’s look at the example below:

# = Operartor
IO.puts "= Operator: Pattern Matching: 1 = 1"
#IO.puts "Pattern Matching:"
IO.puts 1 = 1 
IO.puts "\n"
# == Operator
IO.puts "== Operator"

IO.puts "Same value and same data type: 2 == 2"
IO.puts 2 == 2
IO.puts "\n"

IO.puts "Same value and different data type: 2 == 2.0"
IO.puts 2 == 2.0
IO.puts "\n"

IO.puts "Differnet value and same data type: 2 == 3"
IO.puts 2 == 3
IO.puts "\n"

IO.puts "Differnet value and differnet data type: 2 == 3.0"
IO.puts 2 == 3.0
IO.puts "\n"
# === Operator
IO.puts "=== Operator"

IO.puts "Same value and same data type: 2 === 2"
IO.puts 2 === 2
IO.puts "\n"

IO.puts "Same value and different data type: 2 === 2.0"
IO.puts 2 === 2.0
IO.puts "\n"

IO.puts "Differnet value and same data type: 2 === 3"
IO.puts 2 === 3
IO.puts "\n"

IO.puts "Differnet value and differnet data type: 2 === 3.0"
IO.puts 2 === 3.0
IO.puts "\n"

# != Operator
IO.puts "!= Operator"

IO.puts "Same value and different data type: 1 != 1.0"
IO.puts 1 != 1.0
IO.puts "\n"

IO.puts "Same value and same data type: 1 != 1"
IO.puts 1 != 1
IO.puts "\n"

IO.puts "Different value and same data type: 1 != 2"
IO.puts 1 != 2
IO.puts "\n"

IO.puts "Different value and different data type: 1 != 2.0"
IO.puts 1 != 2.0
IO.puts "\n"

# !== Operator
IO.puts "!== Operator"

IO.puts "Same value and different data type: 1 !== 1.0"
IO.puts 1 !== 1.0
IO.puts "\n"

IO.puts "Same value and same data type: 1 !== 1"
IO.puts 1 !== 1
IO.puts "\n"

IO.puts "Different value and same data type: 1 !== 2"
IO.puts 1 !== 2
IO.puts "\n"

IO.puts "Different value and different data type: 1 !== 2.0"
IO.puts 1 !== 2.0

RELATED TAGS

elixir

CONTRIBUTOR

Azeem Ashraf
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring