if (typeof operand1 != "undefined" && operand1 == operand2) {
}
Above is an if statement that checks for the equality between operand1 and operand2, where operand1 may be undefined under some scenario. I wonder if the typeof operand1 != "undefined" is unnecessary.
Because you use the
==operator and not the===operator, it’s unnecessary only ifoperand2will never benull.But you can change the
if statementto use the===operator which then will pass only ifoperand2“really equals” tooperand1, meaningoperand2isundefinedas well.You can read more about it here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
A fiddle that shows what was written here.
In the
Data Baseworld,null(orundefined) is nothing, which means you can’t compare it to anything.SQL example:
is an error because nothing equals to null, and null doesn’t equal to anything, it just doesn’t exist.
In
javascriptit is not like that, you can compare things tonullandundefined, but you have to be careful withTypeErrors, like with this: