How to code basic JS

First, go to your test site and create a new folder named scripts. Then, within the new scripts folder you just created, create a new file called main.js. Save it in your scripts folder.

Next, in your index.html file enter the following element on a new line just before the closing </body> tag:

<script src="scripts/main.js"></script>

This is basically doing the same job as the <link> element for CSS — it applies the JavaScript to the page, so it can have an effect on the HTML (along with the CSS, and anything else on the page).

Now add the following code to the main.js file:

const myHeading = document.querySelector('h1');
myHeading.textContent = 'Hello world!';

Finally, make sure the HTML and JavaScript files are saved, then load index.html in the browser.

Leave a comment

Design a site like this with WordPress.com
Get started