DEV Community

Cover image for Equality of Data Structures: Ruby vs. JavaScript

Equality of Data Structures: Ruby vs. JavaScript

Anna Rankin on April 07, 2019

Skip to the TL;DR In recent weeks I've been reading through JavaScript Allongé by Reginald Braithwaite (an excellent look at the fundamentals of J...
Collapse
 
nickytonline profile image
Nick Taylor

Same box vs same stuff in the boxes. Great analogy.

Collapse
 
annarankin profile image
Anna Rankin

Thank you Nick! 😁

Collapse
 
ahmaddeel profile image
AhmadDeel • Edited

Great post but two notes:
First => on my personal experience js checks if two objects are the same; I mean if they have the same reference. Like:
Const b = {
value:1
};
Const a = b;
a === b // true

I didn't test that for arrays. And you mentioned that js checks to see if the two thing are equal (in value ? ) sounds to me like js is checking their references. Am I right ?

Second => I think the place of TL;DR should be at the top of this post.
Happy coding.

Collapse
 
annarankin profile image
Anna Rankin

Hi Ahmad - this is correct! For data structures, it does check to see if the two things being compared point to the same reference in memory - for primitives, it compares the items by value. I'd originally had a bit in the article about reference vs identity, but I chopped it out because it was getting convoluted 😳And I think you're right, the TL;DR would be better suited at the beginning. Thanks for your input 🙌

Collapse
 
tsupinie profile image
Tim Supinie

I guess isn't Javascript's == operator supposed to be an "is this the same stuff in the box" check? There's all this implicit typecasting going on, but at its heart, it's an equality check designed to determine if two variables contain the same value. So I'm not sure you can draw the conclusion of philosophical differences in language design when Javascript has an operator with the same intent as in Ruby.

Collapse
 
annarankin profile image
Anna Rankin

Hi Tim, good point! However, if you try out [1,2,3] == [1,2,3] in your console, you'll see it returns false. JS's double-equals will coerce types, but it won't perform a recursive equality check the way it does in Ruby. 😵

Collapse
 
joelnet profile image
JavaScript Joel • Edited

Loving these graphics! Do you make them yourself for these articles?

Collapse
 
annarankin profile image
Anna Rankin

Thank you very much, Joel! 😊 Yes, I do - any chance I get to draw nerdy pixel art, I jump on lol

Collapse
 
joelnet profile image
JavaScript Joel

Please never stop! 😊

Collapse
 
deusmxsabrina profile image
Sabrina

Thanks for the post, as a current Rubyist who is always curious about learning more JS this would have been a total head scratcher for me!

Collapse
 
tammalee profile image
Tammy Lee

Thank you very much for writing this up, Anna. It was very succinct and I loved your illustrations. XD (And the Ditto class. Lol!)

Collapse
 
annarankin profile image
Anna Rankin

Thank you Tammy! I'm glad you liked it 😁

Collapse
 
nahuef profile image
Nahuel

You wrote the whole post just to show off your amazing pixel art skills right?!

Loved them.

Collapse
 
annarankin profile image
Anna Rankin

😁😁😁 made my day! Thanks Nahuel!