What to use it for
It is also used to set and read cookies, perhaps one of its most common uses.
External files Example
The second method is also using the HTML Script element. But this time we take the script, save it as script.js and then we type the filename in an src attribute
Note. See Absolute and Relative paths, to learn how to define file paths.
The content of the script.js file should be as below.
The best inclusion method
It would depend on the situation. But generally its best to keep your scripts in external files, when the same script is used on multiple pages.
This is to take advantage of caching in the browser, which eliminates the need to download these files every time a page using them is requested.