The Safe typeof
luljs 1996 still worksAsk for the type of a variable that was never declared and you get the string undefined, not an error.
In 2026: Reading an undeclared name anywhere else throws a ReferenceError. typeof is the one operator specified to swallow that error and report the string undefined instead. Old scripts leaned on this: typeof x != 'undefined' was the only safe test for whether a global existed before touching it.
Where it came from: Axel Rauschmayer, Speaking JavaScript (2014), the typeof operator chapter. MDN ↗
<script>
document.write('<p><code>typeof neverDeclared</code> is <b>' + (typeof neverDeclared) + '</b></p>');
try {
neverDeclared;
document.write('<p>reading it directly did not throw</p>');
} catch (e) {
document.write('<p>reading <code>neverDeclared</code> directly throws <b>' + e.name + '</b></p>');
}
document.write('<p>typeof is the one operator that tolerates a name that was never declared.</p>');
</script>