There are several ways to run JavaScript but the most popular is to run in inside an HTML in a web page and use a browser to run it.
Adding JavaScript to an HTML Document
Here are a couple of different way to run JavaScript directly in your browser.
JavaScript can be added inside the header
page of the page via a script
tag:
HTML body
JavaScript can also be added inside the body
of an HTML via a script
tag:
External File
JavaScript can also be written inside a dedicated file and then "added" to the HTML page. Given a file named app.js
:
... to include it in an HTML file:
The files will need to reside side by side inside a folder (`my_project` in the example below):
my_project/
├── app.js
└── index.html
ES6 - import & export
As your project grows, the JavaScript code can be split across multiple file. In the example below, we'll use 2 JavaScript files:
To include them both in an HTML file:
Just like before, the files will need to reside side by side:
my_project/
├── my-script.js
├── second-script.js
└── index.html
ES10 - dynamic import with async Early days Google Chrome only