JSFiddle - React, Tailwind, and code Playground
JavaScript
function f(x) {
if (x === undefined) console.log('x is undefined [=== undefined].');
else console.log('x is not undefined [=== undefined.]');
if (typeof(x) === 'undefined') console.log('x is undefined [typeof(x) === \'undefined\'].');
else console.log('x is not undefined [typeof(x) === \'undefined\'].');
// This will throw exception because what the hell is j? It is nowhere to be found.
try {
if (j === undefined) console.log('j is undefined [=== undefined].');
else console.log('j is not undefined [=== undefined].');
} catch (e) {
console.log('Error!!! Cannot use [=== undefined] because j is nowhere to be found in our source code.');
}
// However this will not throw exception
if (typeof j === 'undefined') console.log('j is undefined (typeof(x) === \'undefined\'). We can use this check even though j is nowhere to be found in our source code and it will not throw.');
else console.log('j is not undefined [typeof(x) === \'undefined\'].');
};
console.log('Output from f():');
f();
console.log('------------------------------------------');
console.log('Output from f(null):');
f(null);
console.log('------------------------------------------');
console.log('Output from f(1):');
f(1);