0

I just want that if the valuesToSubmit.personID or valuesToSubmit.dataID == '' is empty , it will enter to if condition and if not it will go to else

if (valuesToSubmit.personID == '' || valuesToSubmit.dataID == ''){
   console.log("the value you've submit is empty")
}else{
 console.log("the value you've submit is not empty")
}

this is the result when i tried to alert valuesToSubmit.personID (empty)

enter image description here

and this is the result when i send some value

enter image description here

this is the result in the console.log() when i tried to enter empty id ( valuesToSubmit.personID )

{}
user14823468
  • 101
  • 13
  • Lots of ways to go about this. You could define a helper function `function isNotEmptyOrNull(inputString) { }` which you can then re-use, or you could use the double negation operator (`!!valuesToSubmit.personId || !!valuesToSubmit.dataID`), or you could explicitly check (`if ((valuesToSubmit.personID === null || valuesToSubmit.personID === '') || (valuesToSubmit.dataID === null || valuesToSubmit.dataID === '')) {`, etc. – nbokmans May 31 '21 at 14:21
  • 1
    Does this answer your question? [How can I check for an empty/undefined/null string in JavaScript?](https://stackoverflow.com/questions/154059/how-can-i-check-for-an-empty-undefined-null-string-in-javascript) – nbokmans May 31 '21 at 14:22

1 Answers1

0

This question is not particular about ReactJS but about JavaScript. Therefore, you should retag the question.

To give an answer to your question:

It depends what value the parameters take when the the user doesn't give an input. The usual options are an empty string '', null or undefined, so checking for falsy values should do it:

if (!valuesToSubmit.personID || !valuesToSubmit.dataID){
 console.log("the value you've submit is empty")
} else {
 console.log("the value you've submitted is not empty")
}

Besides, you should (always) make use of the strict equality operator === for equality comparison as this avoids unwanted type coercion.

alexanderdavide
  • 1,487
  • 3
  • 14
  • 22
  • your condition is wrong, when i tried this, even if i select have value, it always go to else – user14823468 May 31 '21 at 14:30
  • `valuesToSubmit.personID` is false if the value is 0, '', undefined or null - so this part of the condition is wrong, but he pointed out correctly that you should use === for your check – Christian May 31 '21 at 14:31
  • still not entering the if condition – user14823468 May 31 '21 at 14:36
  • The check I provided jumps into `if` if both values are `falsy`. I recommend reading up on truthy/falsy in JavaScript. If `else` is executed, this means values are present. You can try to output the values with `console.log()` to see what's in there. – alexanderdavide May 31 '21 at 14:39
  • the result of `console.log` is `{}` – user14823468 May 31 '21 at 14:55
  • For which value? `personID`, `dataID` or the complete `valuesToSubmit` object? JavaScript considers `{}` as a truthy value. – alexanderdavide May 31 '21 at 15:04
  • the `valuesToSubmit ` – user14823468 May 31 '21 at 15:21
  • Then you do not even have attributes in the object. Is this the intended behavior if no values are set by the user? If so, you can add a condition to the start of the if statement to check if the object is empty: `!Object.keys(valuesToSubmit)`. – alexanderdavide May 31 '21 at 16:15