最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't array === array.toString().split(",") - Stack Overflow

programmeradmin1浏览0评论

This is somewhat similar to split() in javascript, but here's my question, and it's more theoretical than practical

I have an array that looks like this:

var array = ["abc", "def"]

When I do

debug(array === array.toString().split(","));

I get false, I tried == and that also gives false. I did a debug dump of the array and the joined/split array and they look exactly the same in output. What is the difference between them that's causing this to evaluate to false?

I think it's pretty clear for my code that I can just use array without having to do toString.split (it was necessary earlier, I think, not anymore), I'm just curious as to what's going on here.

This is somewhat similar to split() in javascript, but here's my question, and it's more theoretical than practical

I have an array that looks like this:

var array = ["abc", "def"]

When I do

debug(array === array.toString().split(","));

I get false, I tried == and that also gives false. I did a debug dump of the array and the joined/split array and they look exactly the same in output. What is the difference between them that's causing this to evaluate to false?

I think it's pretty clear for my code that I can just use array without having to do toString.split (it was necessary earlier, I think, not anymore), I'm just curious as to what's going on here.

Share Improve this question edited May 23, 2017 at 10:33 CommunityBot 11 silver badge asked Apr 8, 2014 at 16:37 NickolaiNickolai 1,7482 gold badges13 silver badges22 bronze badges 1
  • use array.slice() instead of array.toString().split(",") when needed... – dandavis Commented Apr 8, 2014 at 16:49
Add a ment  | 

5 Answers 5

Reset to default 5

Because array.toString().split(",") returns a new instance of the array. Arrays, being objects, are only equal if they are the exact same instance of the array:

var a = [1,2];
var b = [1,2];
var c = a;
alert(a == c); // true
alert(a == b); // false - not even loose parison can save you

Neither the == nor the === operator will inspect the contents of an array. Rather, they test whether the two operands reference the same array instance in memory. Notice:

['a', 'b'] === ['a', 'b']; // false

var arr = ['a', 'b'];
arr === arr;               // true

In other words, when you're paring two different arrays, == or === will always return false;

Quoting from the MDN Doc for Comparison Operators,

Note that an object is converted into a primitive if, and only if, its parand is a primitive. If both operands are objects, they're pared as objects, and the equality test is true only if both refer the same object.

So, if we pare any two objects, then both == and === will check if they are one and the same. You can check that like this

console.log({} == {});
# false
console.log({} === {});
# false
console.log([] == []);
# false
console.log([] === []);
# false

I believe the reason could be, Arrays and Objects can be nested and it will be very difficult to check if two objects are equal.

The == and === operators do not pare the contents of Arrays. They just check to see if the two Arrays are actually the same Array object.

Array is not a primitive type, they are not the same reference.

var a = ["a"];
var b = ["a"];
console.log(a == b); //false
发布评论

评论列表(0)

  1. 暂无评论