To answer your question, if
will not implicitly take care of null
and undefined
case but, by-default any non-initialized variable in Javascript is undefined
and not the string 'undefined'. So, in your case, 'data' will be undefined
.
You may please check the below to explore and learn more...
let data;
if(data) {
console.log('INSIDE IF: ', data);
} else {
console.log('INSIDE ELSE: ', data);
}
if(data === null && data === undefined) {
console.log('INSIDE IF with Comparison Check: ', data);
} else {
console.log('INSIDE ELSE with Comparison Check: ', data);
}
If you try to execute the above code with strict equality operator (===
), you will see the "INSIDE ELSE with Comparison Check" is called. This is the default way, JS works.