JavaScript Placement

There are two main places to keep your JavaScript code – On Page and External

On Page JavaScript

  1. You can write JavaScript on HTML pages anywhere between <html> and </html> tags.
  2. All on page JavaScript code must be inside <script> tag
  3. In script below, document.write is used to calculate the result of 5+6.
  4. A semicolon ; represents of end of statement in JavaScript.

External JavaScript

  1. You can write all your JavaScript code in an external file and then link that file to any of your HTML pages.
  2. External JavaScript files use a .js extension, for example, myfile.js or external-javascript.js
  3. To use an external JavaScript file, use the <script> tag with src attribute, followed by the path to your external javascript file.
  4. You can include resources in the head tag but now a days it is recommended to place JavaScript just before closing Body Tag.
  5. You can include script at the end of document.