Back to course

Running JavaScript: Internal, External, and Console Execution

JavaScript: The Complete '0 to Hero' Beginner Course

3. Running JavaScript Code

There are three main ways to execute JavaScript code in a web environment.

1. External Script (Best Practice)

We link an external .js file to our HTML document. This keeps the HTML and JS separate and clean.

In index.html:

html

Hello JavaScript!

<!-- The script tag usually goes just before the closing </body> tag -->
<script src="app.js"></script>

In app.js:

javascript console.log('Script is running!');

2. Internal Script

Embedding the JS directly within <script> tags in the HTML.

html

Note: Avoid this method for larger projects.