# When I check if an empty string is in an array, POSTMAN always returns true

**URL:** <https://community.postman.com/t/when-i-check-if-an-empty-string-is-in-an-array-postman-always-returns-true/11067>\
**Category:** Help Hub\
**Tags:** string, array\
**Created:** [February 27, 2020, 1:01pm UTC](https://community.postman.com/t/when-i-check-if-an-empty-string-is-in-an-array-postman-always-returns-true/11067 "2020-02-27T13:01:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vovky](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vovky](https://community.postman.com/u/vovky)\
**Post date:** [February 27, 2020, 1:01pm UTC](https://community.postman.com/t/when-i-check-if-an-empty-string-is-in-an-array-postman-always-returns-true/11067/1 "2020-02-27T13:01:36Z")

</div>

I am using the pre-script method to loop through test data. I am also setting a validation set of data in the prescript file.

```
// fetching the test data
var usernames = pm.environment.get("usernames");
var valid_usernames = pm.environment.get("valid_usernames");

// Setting the test data
if(!usernames){
    usernames = ['ww12345',' ',123,'www.google.com'];
}

// Setting the verification data
if(!valid_usernames){
    valid_usernames = ['ww12345','testing'];
}

// Reseting the loop counter for clicking the 'send' button in top right corner
if(usernames.length === 0){
    usernames = ['ww12345','',123,'www.google.com'];
}

// looping through the test data
var currentUsername = usernames.shift();
pm.environment.set("username", currentUsername);
pm.environment.set("usernames", usernames);

```

Then during the test execution, I am calling the current username, checking if it exists in the validation data, and then checking if the response was 200 or 400.

```
// Checking status codes for valid and invalid users
var valid_usernames = pm.environment.get("valid_usernames");
var username = pm.environment.get("username");
pm.test("Status Code for User", function () {
    if(valid_usernames.includes(username)){
        pm.response.to.have.status(200);
        console.log("Status code 200 for : " + username);
    }else{
        pm.response.to.have.status(400);
        console.log("Status code 400 for : " + username);
    }
    
});

```

The only issue is that I am testing if an empty string returns a 400. However, I do not put an empty string into my validation set, but it returns true as if I did. I’m pretty sure this is because Postman is treating my validation set as a string, rather than an array. And a string always contains an empty string, but an array does not always contain an empty string. You can check for yourself here:  
[Array.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)  
[String.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes)

I believe the behavior is pretty inconsistent. For looping over my data set, Postman operates as if it is an array, but for the validation set, Postman treats it as a string. From what I found, I cannot ‘typecast’ a variable as an array.  
I am looking for a solution that can let me treat valid\_usernames as an array rather than working with it as a string  
I have the following:  
`if(username && valid_usernames.includes(username)){}`

but in the case that i need to check the empty string, then it will be an extra if statement, and I’m trying to stay DRY. Thanks

---

<div class="post-metadata">

**Author:** ![vovky](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vovky](https://community.postman.com/u/vovky)\
**Post date:** [February 27, 2020, 1:14pm UTC](https://community.postman.com/t/when-i-check-if-an-empty-string-is-in-an-array-postman-always-returns-true/11067/2 "2020-02-27T13:14:08Z")

</div>

I also just tried with no success:

in Pre-request Script (after deleting variable manually from environment):

```
var valid_usernames = ['qx18264',456];
pm.environment.set("valid_usernames", JSON.stringify(valid_usernames));

```

and then in Tests:

`var valid_usernames = JSON.parse(pm.environment.get("valid_usernames"));`

for reference, in environments, the variable is set to:  
`"\"\\\"[\\\\\\\"qx18264\\\\\\\",456]\\\"\""`

by no sucess I mean that the following if statement returns true when **username===’ '** , even though I expect it to return false:  
`if(valid_usernames.includes(username)){}`
