Although it is possible to embed JavaScript directly into HTML files, it is generally considered good practice to include as much JavaScript within the code using external files. You have to keep in mind that there is no hard and fast rule regarding this practice; The arguments for using external files are:

  • Maintainability: JavaScript code that is scattered throughout various HTML pages turns code continuation into a problem. So it is much easier to have a directory for most or all JavaScript files so that developers can change JavaScript code free from the markup in which it's used.
  • Caching: Browsers cache are externally linked to JavaScript files based on specific settings, which designates that when two pages are using the same file, the file gets downloaded only once. This eventually leads to faster page-load times.
  • Future-proof: By taking JavaScript as external files, there is no need to use XHTML or comments. The syntax to include external files is the same for both HTML and XHTML.