SmartCodingTips

Your First JavaScript Script

Let’s write and run your very first JavaScript program! 🎉 This simple exercise will help you understand how JavaScript works inside a web page.

🛠️ Step 1: Basic HTML Setup

Create a file named index.html and write the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My First Script</title>
</head>
<body>

    <h1>Hello, JavaScript!</h1>

    <script>
        alert("Hello from JavaScript!");
        console.log("This message is logged in the console.");
    </script>

</body>
</html>

🧪 Step 2: Run It

Open the file in your browser. You should see:

  • An alert box saying "Hello from JavaScript!"
  • If you open the browser’s developer tools console, you’ll see: "This message is logged in the console."

📌 What’s Happening?

  • <script> tells the browser you're writing JavaScript.
  • alert() shows a pop-up message.
  • console.log() writes to the browser's developer console.
Pro Tip: Use Ctrl + Shift + J (or Cmd + Option + J on Mac) to open the browser console and see the output.

🔁 Try This

Modify your script to experiment:

<script>
    let name = prompt("What is your name?");
    alert("Welcome, " + name + "!");
</script>

You’ve just taken your first step into the world of JavaScript. Next, we’ll explore how to work with variables and data types.