JS Where To

Where to Use JavaScript

In HTML Documents

  • Inline scripts: Using <script> tags directly in HTML
  • Internal scripts: Placed inside the HTML <head> or <body>
  • External scripts: Linking via <script src=\"app.js\">

In Various Environments

JavaScript runs in web browsers for front-end, and on servers using Node.js, as well as in desktop and mobile apps powered by frameworks like Electron and React Native.

Best Practices

Loading Scripts

  • defer: Loads scripts after HTML parsing
  • async: Loads scripts asynchronously without blocking parsing

Keeping Code Organized

Structure code into modules and separate files, follow naming conventions, and use build tools or bundlers to manage dependencies and optimize loading.