== not working?

console.log(exists == []); always returns false and

      if(exists == []){
        console.log("Doesn't exist")

always returns exists. But why? Depending on what my other code does exists is either consoled.logged out to be [] or something else. But it always returns exists. Why? If you could help, that’d be great!


Unless exists is an empty array it will always result in false; or log Exists to stdout. If you want to check whether or not the exists variable is positive you can cast it to a boolean: !!exists:

console.log(!!exists ? "Exists" : "Doesn't exist");

EDIT: Because they are two separate pieces of memory. They will never be equal to one-another. They are two different scopes.

Hi there,
Thanks for trying to help. I must have not explained well
If it exists, console.log returns [] but if it doesn’t exist, console.log returns something along the lines of this:
_id: 5ed3acb3adfebe31153e85d4,
statuspagename: ‘Ellis Bot’,
email: ‘emailredacted’,
key: ‘mykey’,
__v: 0
I want to to have an if statement to find whether it contains something or not. But it won’t work. I want to know why

You want to know if a certain object exists in an array?

No, whether anything exists in an array

myArray.length > 1

I’ll try that :slight_smile:

Edit: It worked!!!