I’m inspecting some code and I found something I’d like to run by Javascript veterans. I feel pretty comfortable with Javascript but I’ve always manage to run into something that makes me say, “I didn’t know that about Javascript!”
I’m hoping this is one of those situations:
if (this.props.referralId != prevProps.referralId ||
this.props.referralValidationStatus != prevProps.referralValidationStatus ||
this.props.customerName != prevProps.customerName &&
!!prevProps.personId != !this.props.personId) {
// perform whatever logic is here...
}
My questions:
- Does JS know how to automatically identify the mixture of || and &&? Or is this code missing parenthesis’s around the || comparison?
- Just as it’s explained here, is it fair and should I expect the obvious behavior when comparing a boolean against a truthy/falsy value?
I’m baffled on what the logic should be here. If I were to rewrite this I would do like so:
if ((this.props.referralId !== prevProps.referralId ||
this.props.referralValidationStatus !== prevProps.referralValidationStatus ||
this.props.customerName !== prevProps.customerName)
&& (!!prevProps.personId === !!this.props.personId)) {
// Perform logic
}
However, I’d like to confirm and make sure I’m not missing something I might have missed about JS.
Thank you in advance for confirming my hunch or educating my on something new when it comes to JS. Looking forward to your comments and/or answers.