Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot determine type of undefined()

Tags:

javascript

Recently I investigated a situation in which a programmer inadvertently passed undefined into addEventListener, thus:

window.addEventListener('load', undefined);

No error was thrown. It's as if JavaScript is willing to invoke undefined. But what in the world is undefined()? I have tried all sorts of things, e.g.:

console.log(undefined() === null);
console.log(typeof undefined());

but I never get anything back.

Edit added for clarity: My original question was based on a mistake, as I had not set my Developer tools to log errors to the console. The above two commands (but not the call to addEventListener) DO throw errors in a browser, as answers and comments below indicate.

like image 602
Homer White Avatar asked Oct 30 '22 05:10

Homer White


1 Answers

It's as if JavaScript is willing to invoke undefined.

No, addEventListener is specified to ignore null, which JavaScript’s undefined is converted to. undefined is never called.

Further proof that JavaScript is not willing to invoke undefined, in a browser:

> undefined()
Uncaught TypeError: undefined is not a function
    at <anonymous>:1:1
like image 128
3 revs Avatar answered Nov 13 '22 01:11

3 revs