Skip to content

OutputNot attempted

IIFE shadowing and the typeof trap

const r = 'Dival';
(function () {
const r = 'minal';
console.log(typeof r, r);
if (typeof r === undefined) {
console.log('never');
} else {
console.log('hello world');
}
})();

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

The inner const r shadows the outer one inside the IIFE. typeof ALWAYS returns a string, so comparing it with the value undefined (not the string "undefined") is always false: a classic bug.