Skip to content

HTML Tutorial HTML and JavaScript

JavaScript in HTML: The script Element

Lesson 41 of 81 2 min read By Deepak Paswan Updated Oct 4, 2026

JavaScript makes pages interactive. You add it to HTML with the script element.

Inline script

index.html
HTML
<p id="greeting">Hello.</p>
<script>
  document.getElementById("greeting").textContent = "Hello from JavaScript.";
</script>

The script finds the element with the id greeting and changes its text.

External script

For anything bigger, put the code in a file and load it with src.

index.html
HTML
<script src="app.js"></script>

An external script file is cached by the browser and can be used on many pages. The script tag still needs a closing tag, even with src.

noscript

noscript shows content when JavaScript is turned off or fails to load.

index.html
HTML
<noscript>This feature needs JavaScript. Please turn it on.</noscript>
info
HTML first
A good page works without JavaScript as far as possible. Content, links and forms should be real HTML. Use JavaScript to improve them, not to create them from nothing.
warning
Forgetting the closing tag
<script src="app.js" /> is not valid HTML. Write <script src="app.js"></script>.