Not All = are Evaluated Equally:

Steven Kandow
4 min readJun 29, 2020

--

Exploring the Uses of Single, Double and Triple Equals Sign Expressions in JavaScript and Ruby

When it comes to programming languages, one of the most important symbols that one can utilize is the equals sign (=). As we work with variables, however, it’s important to note that not all expressions with equals signs are created equally. Consider these basic math statements:

x = 6

y = 6

x = y

In the examples above, = is used BOTH to set the variables x and y to a value of 6 AND to state that the two variables are equal in value.

Unfortunately, programming languages do not allow for this same flexibility with the = sign. Depending on what you are trying to accomplish for demonstrating equivalency, it’s important to note how this should be expressed in your programming language of choice. Let’s take a look at how this operates with two programming languages:

JavaScript

In JavaScript, we have three options for expressions that strictly use =. Let’s take a look at some code:

let number = 6
//=> undefined

In the above example, we’re using a single = to assign a value to a variable. Note that in JavaScript, this line is often accompanied by var, const, or let in the front.

What if we want to test the equivalency of the variable number with other things? Let’s see how this plays out using two equals signs:

number == 6
//=> true
number == “6”
//=> true
number == 7
//=> false

What is this testing?

In the first comparison, we know the variable number equals 6, so, of course, this expression results in a true outcome, but what about the second expression?

This should make sense, again, right? 6 and “6” should essentially be equal, but remember that data types matter. The variable number equals a number, not a string. However, “6” is a string. So how are these two values allowed to return true in equivalency even though they’re different data types?

This is because the double equals (==) expression in JavaScript is known as the Abstract Equality Comparison and defines “loose equality.” When JavaScript evaluates this expression, it automatically performs a type conversion so the items on either side of the expression can be evaluated without the data type discrepancy.

Note: Be careful when using the double equals in JavaScript as it can produce some strange results. For example:

const emptyString = ''
//=> undefined
emptyString == 0
//=> true

Let’s add a third equals sign and see how these expressions are evaluated by JavaScript this time.

number === 6
//=> true
number === “6”
//=> false
number === 7
//=> false

The triple equals (===) evaluates Strict Equality Comparison and should be your go-to typically speaking when doing conditionals and comparisons as it will return false if data types are not the same.

Before we leave JavaScript, let’s see what happens if we try to use a single equals sign in a conditional expression.

if (number = 7) { 
console.log(“This should not log because number is set to 6.”)
}
//=> This should not log because number is set to 6.

Uh-oh…what happened here? Isn’t the number variable equal to 6? Let’s check it…

number
//=> 7

Oops.

This is why in JavaScript, you must use either (==) or (===) when doing value comparisons. Using the single equals expression (=) automatically assigns a value to a variable no matter where it appears in the programming.

Ruby

Like JavaScript, Ruby operates differently depending on how many equals signs are present in an expression. Let’s set our number variable in Ruby:

number = 6
=> 6

Easy enough. Now let’s compare with the double equals expression:

number == 6
=> true
number == “6”
=> false
number == 7
=> false

Unlike JavaScript, Ruby’s strict comparison is accomplished with the double equals sign (==) operator. It does not allow for loose equivalency, so the values compared must be the same data type. The exception to this is Ruby’s Float and Integer data types.

Note:

floated_number = 6.0
=> 6.0
number == floated_number
=> true

It is worth noting that Ruby DOES have a comparison operator that utilizes the triple equals sign (===). It tests equivalency on an entirely different level, though.

Let’s observe:

number === 6
=> true
floated_number === number
=> true
Integer === number
=> true
Integer === floated_number
=> false

Woah…! What’s going on here?

The triple equals operator (also known as the “threequals” operator) can check a few things:

  1. Is the thing on the left equivalent to the thing on the right (functions like the double equals expression (==)?
  2. Is the thing on the right an example of the data type of the object on the left?
  3. Does the number of the left fall within the range presented on the right?
  4. Does the string on the left include the regex on the right?

We saw the first two in play in the examples above. We know that number and floated_number are equivalent according the double equals comparison operator.

The third and fourth examples are testing comparison of item #2.

The variable number is of the data type Integer, so the expression returns true. However, floated_number is of the data type Float, not Integer, so the threequals expression here returns false.

Let’s look at a few more expressions to see items #3 and #4:

(2..8) === number
=> true
(1..5) === number
=> false
number_as_string = “six”
=> "six"
Integer === number_as_string
=> false
String === number_as_string
=> true
/i/ === number_as_string
=> true

One last item worth noting regarding the “threequals” operator. With the exception of item #1 above, placement MATTERS when evaluating the expression. Consider the following:

Integer === number
=> true
number === Integer
=> false
(2..8) === number
=> true
number === (2..8)
=> false
/i/ === number_as_string
=> true
number_as_string === /i/
=> false

As you can see, not all expressions in programming using strictly the equals sign are created equally, but using them properly, you can assign values to variables and test equivalency (or other aspects of data).

Happy coding!

--

--