Equality of Data Structures: Ruby vs. JavaScript
Tags: ruby, javascript, programming
Published: 2019-04-07 15:48:12

---
title: Equality of Data Structures: Ruby vs. JavaScript
published: true
description: A look at how the comparison operators in JS and Ruby differ
tags: ruby, javascript, programming
cover_image: https://thepracticaldev.s3.amazonaws.com/i/yton8kv4bwau0ryjcf29.png
---

[Skip to the TL;DR](https://dev.to/annarankin/equality-of-data-structures-ruby-vs-javascript-48b7#tldr)

In recent weeks I've been reading through [JavaScript Allongé](https://leanpub.com/javascriptallongesix) by Reginald Braithwaite (an excellent look at the fundamentals of JS through a functional programming lens) with a coworker. My coworker brought up something that didn't make sense to her:

> In JavaScript, why isn't an empty array equal to an empty array? What's the difference between the two identical objects?

![](https://thepracticaldev.s3.amazonaws.com/i/sq6l3flghhxaxft9ua9a.png)

To someone who's been working with JavaScript for a while, the answer might seem straightforward: "They're two different arrays; of course one is not equal to the other!" A Rubyist like my friend, however, may see it differently: "These two arrays have the same content—what do you _mean_ they're not equal?" As I see it, this is a philosophical difference between the meanings of the comparison operators in Ruby and JavaScript. One language uses the concept of object _equivalence_ to compare data structures, while the other checks explicitly for object _identity._

_Note: I'm not going to discuss the [JS double equal](https://codeburst.io/javascript-double-equals-vs-triple-equals-61d4ce5a121a) or [Ruby's threequal](https://stackoverflow.com/a/4467823) operators, as these are more than just simple comparison operators (I also avoid using them because they can be confusing and misleading!)._

![Pixel art version of the Ruby programming language's logo](https://thepracticaldev.s3.amazonaws.com/i/m0qtdk4jjp60buay16ke.png)

## Equality of Data Structures in Ruby

When you compare data structures using the built-in behavior of `==` in Ruby, you're really comparing the _contents_ of the object—and in the case of an array, you're also checking that the order of the elements is the same in both. This means that two variables that point to different objects in memory **could be equal.**

```ruby 
> first_array, second_array = [1,2,3], [1,2,3]

> first_array == second_array
=> true
```

If for some reason you truly want to check and see that two variables refer to the same object, you can check object's ID—or preferably, use the `.equal?` method:

```ruby
> first_array, second_array = [1,2,3], [1,2,3]

> first_array.object_id == second_array.object_id # 70176467875700 == 70176467875680
=> false
> first_array.equal?(second_array) # a clearer way to perform this comparison
=> false
```

Unlike in JavaScript, the comparison operator in Ruby is actually a method defined on the class you're comparing (awesome walkthrough of this concept [here](http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/105-equality_of_objects)). Since `==` is just a method, you can even override it if you like! This is a reasonable practice if you're writing custom classes that need to be compared to each other. Silly example below:

```ruby
> class Ditto
*   def self.==(other)
*     true
*   end
* end

> Ditto == 'Pikachu'
=> true
> Ditto == 2
=> true
> Ditto == false
=> true
```

![Pixel art version of the JavaScript Logo](https://thepracticaldev.s3.amazonaws.com/i/q4mb7fi1ukyk6o9vqt96.png)

## Equality of Data Structures in JavaScript

Unlike Ruby, JavaScript does not expose a unique ID for objects because it doesn't _need_ to. Data structures are compared by identity by default. If two variables are equal, you can be sure that they point to the same object in memory.

```javascript
> const firstArray = [1,2,3]
> const secondArray = [1,2,3]
> firstArray === secondArray
false
```

If you want to check and see if two separate data structures have the same content, you'll have to write your own logic to check—or use a function from a library like [Lodash](https://lodash.com/docs/4.17.11#isEqual).

```javascript
// Super naïve implementation:
const arraysAreEqual = (array1, array2) => {
  return array1.every((el, index) => el === array2[index])
}
> arraysAreEqual([1,2,3],[1,2,3])
true
> arraysAreEqual([1,2,3],['a','b','c'])
false
```

# TL;DR

**JavaScript's `===`** checks to see if the two variables it's comparing _point to the same data structure,_ while **Ruby's `==`** method checks to see if the _contents_ of two arrays or hashes are equivalent.

![RB vs. JS Equality - first image of two closed boxes & JavaScript logo, text "Is this the same box?" Second image of two open boxes with the numbers 1,2, and 3 in each, text "Is the same stuff in this box?"](https://thepracticaldev.s3.amazonaws.com/i/arqmcciw9n5tfekra6yt.png)

Ruby's `[1,2,3] == [1,2,3]` translates to `[1,2,3].every((el, index) => el === [1,2,3][index])` in JS.

JavaScript's `[1,2,3] === [1,2,3]` translates to `[1,2,3].equal?([1,2,3])` in Ruby.

## References

Hopefully this helps you wrap your mind around what the two different languages expect when comparing data structures! If you're interested in going deeper, I've put together some references:

- [MDN docs on equality and sameness in JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Strict_equality_using)
- [A fun JavaScript equality table](https://dorey.github.io/JavaScript-Equality-Table/) (bonus points for the `if()` explanations!)
- [Intro to Ruby's Comparison Operators](http://ruby-for-beginners.rubymonstas.org/operators/comparison.html)
- [RubyMonk's custom `.==` method example](http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/105-equality_of_objects)