Going from web2py -> JavaScript is easy:
{{=var}}
However, going from JavaScript -> web2py requires an AJAX call. Since
JavaScript is run on the user's machine and not the web server, the two
machines have to communicate somehow, which is where AJAX comes in. You
should also know that things inside {{ }} will NOT run on the user's
machine. So if you are expecting something in these blocks to happen after
the page loads, it won't work and you have to use AJAX.

