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.

Theory in plain words

Event to actionUser clicksEvent firesScript runsPage updates
Event to action

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
Show an alertTry it Yourself
Change textTry it Yourself
Script elementTry it Yourself
CounterTry it Yourself
Change colourTry it Yourself
Add a list itemTry it Yourself
Read an inputTry it Yourself
Live typingTry it Yourself
Show the timeTry it Yourself
Simple calculatorTry it Yourself
Toggle classTry it Yourself
Mouse and keyboard eventsTry it Yourself
Random numberTry it Yourself
Noscript messageTry it Yourself
Template elementTry it Yourself

Quick quiz

1. Which tag holds JavaScript?

2. Which attribute runs code on click?

3. Where do scripts often go?

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.

My notes
Ask a question about this lesson