Login

Sign Up

JavaScript in HTML: Inline, Internal & External
Priyanshu Sharma

Posted on Jun 11, 2025 | Frontend

JavaScript in HTML: Inline, Internal & External

Hey Devs!

This is my very first blog on frontend development, and I'm super excited to share it with you! 🎉

If you're someone who finds coding scary or confusing, don't worry — I’ve kept things fun, simple, and beginner-friendly (with a pinch of humor 😄). Today, we’re talking about how JavaScript makes friends with HTML — in three different styles!

Let’s explore the magic behind buttons, clicks, and pop-ups. 💻✨
Hope you enjoy reading it as much as I enjoyed writing it!

So you’ve met HTML — the calm, structured person who builds webpages like LEGO blocks 🧱.
But then comes JavaScript — the energetic buddy who adds fireworks, interactivity, and a lot of "oomph" to your website! 💥

But how do you introduce this wild JavaScript to the well-mannered HTML?
Well... there are three ways — kind of like dating styles. Let’s break them down.

You’ll see some things like function and alert() in the examples — and if those words sound alien to you, that’s totally okay! 🚀
This is just the beginning of our JavaScript journey, and we’ll be learning everything step by step in future blogs. So for now, just focus on the idea, not the code. 😉


1. Inline JavaScript — The Flirty Text Message 📱

This is when HTML and JavaScript are so close, they text each other constantly — right there in the same tag!
JavaScript code is written directly within an HTML element's attribute, such as onclick.

<!DOCTYPE html>
<html>
<head>
    <title>Inline JS Example</title>
</head>
<body>
    <button onclick="alert('This is an example of alertbox!')">Click Me</button>
</body>
</html>

🗯️ “Hey JS, can you pop up a message when someone clicks me?”

Example

✔️ Super quick
❌ Gets messy if you do this too much (like you are writing JavaScript code in every HTML line)


2. Internal JavaScript — The Private Chat 🛋️

Now, HTML and JavaScript are in the same file but JS moves into a special <script> tag. It’s like a cozy private chat room 🫶.

<!DOCTYPE html>
<html>
<head>
  <title>Internal JS Example</title>
  <script>
    function greet() {
      alert('Hello from internal JS!');
    }
  </script>
</head>
<body>
  <button onclick="greet()">Say Hi</button>
</body>
</html>

✔️ More organized
✔️ Good for small websites
❌ Still not great if things get too big — like too many guests in a small room.


🌍 3. External JavaScript — The Long-Distance Relationship 📦

Now, HTML and JavaScript are in separate files. HTML sends a neat little invite like:

<!DOCTYPE html>
<html>
<head>
    <title>External JS Example</title>
    <script src="script.js"></script>
</head>
<body>
    <button onclick="showMessage()">Click Me</button>
</body>
</html>

And your script.js file might look like:

function greet() {
  alert("Hi from a whole other file!");
}

It’s like JS has its own apartment but still shows up when called. 💁

✔️ Super clean and reusable
✔️ Best for big projects
✔️ You can use the same JS on multiple pages
❌ One extra file to manage, but totally worth it!


🎯 So, What Should You Use?

If you’re just starting out:

  • Try internal for learning 🧠
  • Move to external when things get serious 🧰

Avoid inline unless you're just testing stuff (or want your code to look like spilled coffee ☕).


🚀 Ready to Try?

Now that you know the three styles, go ahead and write some JavaScript love notes to your HTML pages! 💌

Remember: Code is just you talking to the browser. And the browser? It's a good listener (most of the time 😅).

Until next time — stay curious and keep clicking those buttons! 🔘✨

Happy Learning!

4 Reactions

0 Bookmarks

Read next

Priyanshu Sharma

Priyanshu Sharma

Dec 14, 24

3 min read

|

How to Setup & Install VS Code: A Beginner’s Guide

Priyanshu Sharma

Priyanshu Sharma

Dec 17, 24

6 min read

|

Essential VS Code Extensions for Developers