I\'ve come across the following code:
function test(data) {
if (data != null && data !== undefined) {
// some code here
}
}
In your case use data==null
(which is true ONLY for null and undefined - on second picture focus on rows/columns null-undefined)
function test(data) {
if (data != null) {
console.log('Data: ', data);
}
}
test(); // the data=undefined
test(null); // the data=null
test(undefined); // the data=undefined
test(0);
test(false);
test('something');
Here you have all (src):
if
== (its negation !=)
=== (its negation !==)
An “undefined variable” is different from the value undefined
.
An undefined variable:
var a;
alert(b); // ReferenceError: b is not defined
A variable with the value undefined
:
var a;
alert(a); // Alerts “undefined”
When a function takes an argument, that argument is always declared even if its value is undefined
, and so there won’t be any error. You are right about != null
followed by !== undefined
being useless, though.