As your application - typically a portlet - is deployed in pages that contain other applications, it is very important that your JavaScript code is safe from other code, and vice versa, does not harm global variables.
Here are some tips to write your JavaScript code safely:
Follow GMD module patterns as much as possible.
Avoid writing inline scripts if possible. Avoid including libraries externally (in script tag) if possible.
When using JQuery, use the built-in shared module "jquery" if possible. Use GMD adapter configuration if you need other JQuery versions and extensions.
It is strongly recommended you follow these tutorials:
Adding JavaScript to a portlet - a quick tutorial with code sample.
JavaScript Development, GateIn Reference - the GMD guideline.