JavaScript cleanup

Mostly:

- Adding missing semicolumns
- Making comparisons typesafe
- Fix variables used out of scope

And some other minor fixes.