alert('Hi!')
is a function call that alerts 'Hi' and returns nothing (undefined
).
onclick
expects to get a function and you are passing the function call's result which is undefined
.
Since JavaScript
is not a strong typed framework you don't get an error on bad assignments.
So why does the following work:
It's because the html parser (in this case, your browser) does some work behind the scenes and wraps the call with a function.