-2

I have been learning about mutability in Python, Ruby and Javascript. My assumption was that Ruby and Python would behave similarly to Javascript when using the comparison operator between two arrays or two objects of the same value. Why are Python and Ruby returning true when comparing arrays of same values but Javascript does not?

For example:

Python:

>>> x = [1,2,3,4,5]
>>> y = [1,2,3,4,5]
>>> x == y
True

Ruby:

> x = [1,2,3]
> y = [1,2,3]
> x == y
=> true

Javascript:

> x = [1,2,3]
> y = [1,2,3]
> x === y
false
Joel Hoelting
  • 1,862
  • 2
  • 23
  • 45
  • 2
    Because arrays are objects in JS, and no 2 objects are equal. – Keith Apr 01 '21 at 16:01
  • 1
    Triple equal sign does not care about values but the objects themselves, that is to say, if the variables point the same memory space – TUI lover Apr 01 '21 at 16:01
  • https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript here can be detailed info for JS arrays comparing – Yurii Verbytskyi Apr 01 '21 at 16:05
  • 1
    @TUIlover `x == y` would also be considered false in JavaScript. My understanding is that three equals is a strict type comparison operator. – Joel Hoelting Apr 01 '21 at 16:06
  • @JoelHoelting it simply because javascript decide to do this. And one cannot provide equality operator in javascript custom class either. – apple apple Apr 01 '21 at 16:07
  • 2
    "My assumption was that Ruby and Python would behave similarly to Javascript when using the comparison operator" bad assumption – juanpa.arrivillaga Apr 01 '21 at 16:11

1 Answers1

0

Why are Python and Ruby returning true when comparing arrays of same values but Javascript does not?

Because they are three completely different programming languages, and there is no reason to expect that they behave the same.

ECMAScript does what the ECMAScript Language Specification says it should do. Ruby does what the Ruby Language Specification says it should do. Python does what the Python Language Specification says it should do.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653