0

I'm trying to check for an empty string array like this:

var array = ['',''];
//Other code
if (array == ['',''])
    //Do stuff

and I've found that

['',''] == ['','']

returns false. Why is that? What method should I use to check for an empty string array? Is there a way where I don't have to check each individual item?

derekantrican
  • 1,891
  • 3
  • 27
  • 57

5 Answers5

4

Beside the proposed using of Array#toString method, I suggest to use Array#join with an empty string as separator and then test the result. The advantage is, it works for an arbitrary count of elements inside of the array.

var bool = array.join('') ? 'not all empty string' : 'all empty string';
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

['', ''] == ['', ''] returns false because in JavaScript arrays are objects, and objects in JavaScript have reference semantics. Comparing objects to each other actually compares their reference IDs, which will be different for different references. So, even though both sides of == are the "same" array, they are different references.

If you want to check that an array only contains empty strings, use Array.prototype.every as in the following:

myArray = ['']
console.log(myArray.every(el => el === '')) // true
myArray = []
console.log(myArray.every(el => el === '')) // true
myArray = ['test']
console.log(myArray.every(el => el === '')) // false

If you are in an environment without ES6 support, you can swap the el => el === '' for function(el) { return el === '' }.

Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
0

You can do this:

var arr = ["","", ""].reduce(function (a, b) {
  return a + b;
});

if(arr == "") {
console.log('Array is empty');
}
Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18
  • Your `reduce` is just a clumsy way to write `join`. Your code also won't work if the input array has no elements. –  Apr 27 '17 at 17:33
  • @torazaburo Point taken but the OP, didn't talk about empty array,, in the sense that array has no elements. What is your suggestion to make it better? – Pankaj Shukla Apr 27 '17 at 17:39
  • `var arr = ["", "", ""].join('');`. –  Apr 27 '17 at 18:06
  • My idea was towards reducing the array to single element,which what join() is doing here. Also, there are some studies that have been done that suggest that in modern browsers string concatenation is much faster than join() method. Hence I chose reduce(), which is doing string concatenation. These threads have such discussions: http://stackoverflow.com/questions/7299010/why-is-string-concatenation-faster-than-array-join Another one, not exactly same but similar from Kyle Simpson, not so long ago https://davidwalsh.name/combining-js-arrays – Pankaj Shukla Apr 27 '17 at 18:11
0

This should also work:

var arr = [""];
console.log(String.valueOf(arr[0]) === String.valueOf(''));
lczapski
  • 4,026
  • 3
  • 16
  • 32
0

Based on the answer from Nina above this should also work!

 let arr1 = ['']
 const bool1 = !!arr1.join(''); // false
 console.log(bool1)
            
 let arr2 = ['String']
 const bool2 = !!arr2.join(''); // true
 console.log(bool2)