Home / Learn HTML / HTML and JavaScript
HTML and JavaScript
JavaScript makes pages react to clicks, typing and time. The script element holds your code.
Definition
JavaScript is the programming language of the browser. It reacts to clicks, changes the page and talks to servers. You add it with the script element, either with code inside or with src pointing to a file.
- Put scripts at the end of the body or use
defer, so the page shows first. - JavaScript finds elements with
document.getElementByIdordocument.querySelectorand changes them. - Use
<noscript>for a message when JavaScript is off.
Theory in plain words
What it is
JavaScript adds behaviour to a page. You add it with the script element, inline or from a file.
Why we use it
Without it pages are static. With it they react to clicks, check forms and load data.
How it works
The browser downloads the script and runs it. defer waits until the HTML is parsed, which keeps the page fast.
Where it is used
- Menus and sliders
- Form checks
- Loading data from APIs
Quick quiz
1. Which tag holds JavaScript?
script holds code.
2. Which attribute runs code on click?
onclick handles clicks.
3. Where do scripts often go?
So the page loads first.
Common questions
Which tag holds JavaScript?
<script>. script holds code.
Which attribute runs code on click?
onclick. onclick handles clicks.
Where do scripts often go?
End of body or with defer. So the page loads first.