Because you use the ==
operator and not the ===
operator, it's unnecessary only if operand2
will never be null
.
But you can change the if statement
to use the ===
operator which then will pass only if operand2
"really equals" to operand1
, meaning operand2
is undefined
as 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 Base
world, null
(or undefined
) is nothing, which means you can't compare it to anything.
SQL example:
SELECT *
FROM table_name t
Where t.operand1 = null
is an error because nothing equals to null, and null doesn't equal to anything, it just doesn't exist.
In javascript
it is not like that, you can compare things to null
and undefined
, but you have to be careful with TypeError
s, like with this:
var x;
x.foo; // TypeError!