2

I'm trying to achieve a condition as in Case 1 which works ok. Is the Case 2 similar? Is there any other better way to write this condition?

Case 1:

if (selectedItem === 'firstName' || selectedItem === 'lastName') {
...
}

Case 2:

if (selectedItem === ('firstName' || 'lastName')) {
...
}
Federick J
  • 478
  • 5
  • 16
  • 2
    "*Is the Case 2 similar?*" no `('firstName' || 'lastName')` resolves to `'firstName'` thus the second code is effectively `selectedItem === 'firstName'`. And before you ask, `selectedItem === 'firstName' || 'lastName'` wouldn't "fix" it, since it's now the same as `(selectedItem === 'firstName') || ('lastName')` - it doesn't matter what the equality expression results in - whether true or false, it will be ORed with a truthy value. `true || 'lastName'` and `false || 'lastName'` both have a truthy result. – VLAZ Nov 03 '22 at 07:28

0 Answers0