JavaScript can be embedded almost anywhere in an HTML document using the <script> tag.
Here’s a basic example:
<!DOCTYPE html>
<html>
<body>
<p>Before the script...</p>
<script>
alert("Hello, world!");
</script>
<p>...After the script.</p>
</body>
</html>📌 The code inside the <script> tag is automatically executed when the browser processes it.
The <script> tag has a few attributes that are rarely used nowadays but can still be found in old code:
<script type="text/javascript">- In HTML4, the
typeattribute was required. - Typically set as
type="text/javascript". - In modern HTML, it’s optional.
- Today, it’s used for JavaScript modules, like
type="module", but we’ll cover that later.
<script language="JavaScript">- This was meant to specify the script’s language.
- It's now deprecated—JavaScript is the default.
- You don’t need to use it anymore.
In really old tutorials, you might see something like:
<script type="text/javascript">
<!--
alert("Hello, old world!");
//-->
</script>- These were used to hide JS from old browsers.
- Totally unnecessary today.
- Can be a sign of very old code.
If your JS code is large or reused, it’s best to keep it in a separate file.
<script src="/path/to/script.js"></script>- The path can be absolute (
/path/to/) or relative (./script.js). - You can also use a full URL:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>To include several external scripts, use multiple <script> tags:
<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>- Only simple scripts are written directly in HTML.
- More complex or reusable code goes in external files.
- Browsers cache external files, which improves performance and reduces bandwidth usage.
This won’t work:
<script src="file.js">
alert(1); // ❌ ignored!
</script>Use separate <script> tags instead:
<script src="file.js"></script>
<script>
alert(1); // ✅ works
</script>