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... [Read Full]
markdown guide
 
 

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.

 

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 πŸ™Œ

 

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.

 

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. 😡

 

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

 

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

 
 

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!

 

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

Loved them.

 

😁😁😁 made my day! Thanks Nahuel!

 

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

 

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

code of conduct - report abuse