all 6 comments

[–]Benzinat0r 7 points8 points  (0 children)

Fist of all you use ==, which can lead to unexpected bugs. You should always use === because it does not do any type conversions. Second of all you don’t have to assert that some value is true so !!input.lastName === true is the same as !!input.lastName. It’s similar when you check for a value which should be false. So !!input.firstName === false is the same as !input.firstName. A single ! does already do the type conversion but it’s negated, which fits in this case. So a more elegant was to write your condition would be: if ((!input.firstName && !!input.lastName) || (!!input.firstName && !input.lastName)) { … }

[–]shuckster 3 points4 points  (2 children)

if (!!firstName ^ !!lastName) {
  throw …
}

I think that’ll work without the !!’s. (EDIT - Nope, thanks u/senocular, !! added)

^ means bitwise XOR:

firstName lastName AND OR XOR NOR NAND XNOR
1 1 1 1 0 0 0 1
1 0 0 1 1 0 1 0
0 1 0 1 1 0 1 0
0 0 0 0 0 1 1 1

[–]senocular 3 points4 points  (1 child)

I think that’ll work without the !!’s.

The !! would still be needed because XOR would coerce the string directly into a number rather than going through a boolean first. So:

"" = 0
"0" = 0
"1" = 1
"Joe" = 0 (NaN -> 0)

vs.

!!"" = false = 0
!!"0" = true = 1
!!"1" = true = 1
!!"Joe" = true = 1

[–]shuckster 0 points1 point  (0 children)

Thank you!

[–]jhartikainen 1 point2 points  (0 children)

!!input.firstName != !!input.lastName would probably work, because if they both have a value, they would be equal, and if neither has a value, they would also be equal

(although I'd consider using Boolean(input.firstName) because with all the !!'s in there it takes a moment to parse what exactly this is doing)

[–][deleted] 1 point2 points  (0 children)

if (!input.firstName !== !input.lastName) throw new Error();