0

Guys something is really bothering me.

In JavaScript - Arrays are objects, meaning each arrays is allocated with a piece of memory for that datatype.

So that makes sense when

arr1 = [1,2,3]
arr2 = [1,2,3]

arr1 == arr2 returns false

HOWEVER

In php that same scenario returns true.

Why is that the case.

2
  • the == and === are treating your variables as string , so by doing $ar1.toString() == $ar2.toString() you cna compare it in js. how ever it will not work if the order of element are not same. Commented Jan 24, 2017 at 2:05
  • you cna use developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… to solve this problem when you are comparing two arrays in js. Commented Jan 24, 2017 at 2:06

1 Answer 1

1

Here is why this returns false in Javascript:

When you create these 2 arrays:

arr1 = [1,2,3];
arr2 = [1,2,3];

you instantiate 2 different Array objects see Array reference. So, even if they have the same elements, they are not the same object, so it returns false.

if you create only one object and copy the reference to another variable, like this:

var arr1 = [1,2,3];
var arr2 = arr1
(arr1 == arr2) //returns true

it will returns true because they have the reference to the same object([1,2,3]).

I think that you are familiar with O.O., if is not the case, please take a look at this: Object Oriented Programming

So, if you need to compare if each element of an array is equal to another in the same index you use the native function every() as @Prafulla Kumas Sahu mentioned. every doc.

Here is a naive example of how you could compare if 2 arrays have the same elements using every():

var arr1 = [1,2,3];
var arr2 = [1,2,3];

arr1.every(function(value, index){
    return value == arr2[index];
}); 
//returns true

In PHP there are extra native operators for Arrays in the PHP language, php docs. They can check:

  • $a + $b Union Union of $a and $b.
  • $a == $b Equality TRUE if $a and $b have the same key/value pairs.
  • $a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
  • $a != $b Inequality TRUE if $a is not equal to $b.
  • $a <> $b Inequality TRUE if $a is not equal to $b.
  • $a !== $b Non-identity TRUE if $a is not identical to $b.

So, it's false in javascript because the operator == check if the instance of an Array object has the same reference to another.

And it's true in PHP because there are extra operators for arrays, and the operator == check if two different arrays have the same pair value.

Sign up to request clarification or add additional context in comments.

7 Comments

a very good explanation, thank you @tfidelis, but i think the OP understand that, but how about PHP ? is it the same case for PHP ? if yes, why PHP return true for the same scenario ?
Thanks great explanation. But the question is, why in PHP when comparing 2 object arrays the return is true? or is it in PHP they are not object arrays they are primitive values.. hmmm..
@JohnBryant you are right, i will edit the answer and tell you why is true in php.
So array in all languages is an object array? They are not primitive data types correct?
yes, every language implements in its own way. has its own methods to deal with, operators and so on. The idea of array is the same.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.