-2

In my current project I am taking a request parameter with a value of either array or a string. But if I get an array it would be an empty array. So what I did is I checked the type first and then I worked with the value. But then I did something like this

const reqParam = []
if (reqParam === []) {
    console.log('empty array')
} else {
    console.log('string')
}

But reqParam despite of being an empty array is giving me false while comparing with []. Why it is behaving like this? Thanks in advance.

Dev Now
  • 1
  • 1
  • 1
    Why you don't check the length array? if (reqParam.length === 0) – IsraGab Aug 02 '18 at 19:35
  • 3
    @IsraGab That's not the logic the OP is looking for. They want to know if it's an empty array, if not, they are treating it as a string. `reqParam = [42]` should probably not be treated as a string. That being said, `if (Array.isArray(reqParam) && reqParam.length === 0)` would suffice for an empty array check – mhodges Aug 02 '18 at 19:36
  • @mhodges, I don't get you. An empty array is array which its lenght is 0, isn't it? – IsraGab Aug 02 '18 at 19:38
  • @IsraGab Yes but so is the string `""`. it also has a length of 0. – mhodges Aug 02 '18 at 19:38
  • @mhodges - post your solution as the answer. :) – Jamie Weston Aug 02 '18 at 19:40
  • 3
    @JamieWeston I would, but this question is a duplicate and should be referred to the answers there – mhodges Aug 02 '18 at 19:41
  • 1
    @IsraGab You are right but if string is empty, I mean `reqParam = ""` then `reqParam.length` would be 0 too. – Dev Now Aug 02 '18 at 19:42
  • 1
    By the way `reqParam == ""` would evaluate to true :) (yup, comparisons in JS can be fun) – Jonas Wilms Aug 02 '18 at 19:44
  • @DevNow You can see my comment above as to how to check for an empty array. In either case, you will need another condition in your if/else, because if `reqParam` **is** an array, but **not** empty, you will fall into your else check and it will treat it as a string. – mhodges Aug 02 '18 at 19:44
  • @IsraGab Not exactly. A non-empty array would also fall into the "else" check, which would treat it as a string, which is probably not what is intended. – mhodges Aug 02 '18 at 19:47
  • The answer would probably be more [like this](https://repl.it/@ugam44/PleasedInformalStructure) – mhodges Aug 02 '18 at 19:51
  • if(Array.isArray(reqParam ) && reqParam .length ==0) else if(typeof reqParam == "string"). That answer is ok for 100% – IsraGab Aug 02 '18 at 19:56

1 Answers1

-1

Your code creates two different arrays, that's why the comparison is returning false. === will only compare their references and not their content.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 1
    You're right, but even `reqParam == []` will return false. This is not the answer to the question. – Jamie Weston Aug 02 '18 at 19:39
  • 1
    @mhodges If you have taken time to read through the site's rules, you would have known that answering on a post that hasn't been marked as a duplicate is okay. Not only that, there is only one person who think it's a duplicate as of now. – Derek 朕會功夫 Aug 02 '18 at 19:40
  • 1
    @Derek朕會功夫 You also have moderation privileges that give you a responsibility to flag questions as duplicates instead of answering them. – mhodges Aug 02 '18 at 19:41
  • 1
    @mhodges Yes I do, but I am not going to because those two questions are not solving the same problem. OP is trying to check if the array is indeed empty from the request instead of comparing two arrays too see if they have the same content. – Derek 朕會功夫 Aug 02 '18 at 19:42
  • 1
    *(opinion)*: I disagree to both statements. This should be marked as dupe as it does not provide any info for further visitors, but answering is still okay as it helps the OP more than a few pages of explanations – Jonas Wilms Aug 02 '18 at 19:43
  • @JonasW. That's a fair assessment. I think the duplicate questions would give the OP enough information to know why the equality check is not working as intended. I suppose this could also be a duplicate of how to check if a variable is an array. Seems like that is more what the OP is looking for? – mhodges Aug 02 '18 at 19:48