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?

15
  • just checking or compairing with another array? Commented Apr 27, 2017 at 17:07
  • 1
    Possible duplicate of How can I check JavaScript arrays for empty strings? Commented Apr 27, 2017 at 17:08
  • Are you checking for a single element having empty string as value? Commented Apr 27, 2017 at 17:09
  • @NinaScholz The empty array I'm comparing against is always constant. Basically, I want to see if array contains only empty strings Commented Apr 27, 2017 at 17:19
  • @guest271314 I would like to check the whole array at once (if this is possible in JavaScript) rather than creating another loop to check each item, one at a time Commented Apr 27, 2017 at 17:20

5 Answers 5

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';
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one. Hadn't thought of using join.
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 === '' }.

1 Comment

I am using Google Apps Script and this doesn't seem to work there: i.imgur.com/CLteUor.png
0

You can do this:

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

if(arr == "") {
console.log('Array is empty');
}

4 Comments

Your reduce is just a clumsy way to write join. Your code also won't work if the input array has no elements.
@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?
var arr = ["", "", ""].join('');.
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: stackoverflow.com/questions/7299010/… Another one, not exactly same but similar from Kyle Simpson, not so long ago davidwalsh.name/combining-js-arrays
0

This should also work:

var arr = [""];
console.log(String.valueOf(arr[0]) === String.valueOf(''));

Comments

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)

Comments

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.