mc0de
JavaScript · Lesson 01 of 17

Introduction to JavaScript

Bring your websites to life! Discover how JavaScript transforms ordinary pages into interactive experiences that respond to clicks, display messages, and react to the people using them.

JavaScript Getting Started Interactive Websites Hands-On Practice
● ● ●   hello.js
const message = "Hello, JavaScript!";

console.log(message);
CONSOLE OUTPUT
> Ready to run…
01 / Getting Started

Meet JavaScript

You’ve already learned how HTML builds a page and CSS makes it look good. Now it’s time to make that page respond. JavaScript lets you change words, react to clicks, check answers, and create experiences that feel alive.

Think about a button that reveals a message. HTML creates the button, CSS styles it, and JavaScript makes something happen when it’s clicked.

See it for yourself

Click the button below to see JavaScript in action.

Click the button to reveal a greeting.

Remember: HTML is structure, CSS is presentation, and JavaScript is behavior. They work together to create interactive websites.
02 / Your First Command

Say Hello to the Console

Let’s begin with a single instruction. The browser’s console is a place where developers can see messages from their code.

hello.js
console.log("Hello, JavaScript!");

console.log() prints a value in the developer console. The words inside quotation marks are a string — a piece of text.

The semicolon ends the statement. JavaScript often works without it, but using it consistently can make code easier to read.

Try it: In Chrome or Edge, open Developer Tools with F12, select Console, and enter the line above. The message appears when you press Enter.
03 / Adding JavaScript to HTML

Give Your Page Instructions

JavaScript can be placed inside an HTML <script> element. When the browser reaches that element, it runs the code.

index.html
<h1>My First JavaScript Page</h1>

<script>
  console.log("My page is ready!");
</script>

For larger websites, developers usually keep their JavaScript in a separate file and link to it with <script src="app.js" defer></script>.

The defer attribute lets the browser finish parsing the page before running the script.

04 / Finding Elements

Change What Visitors See

Printing to the console is useful, but what if you want to change something on the page? JavaScript can find an element by its ID and replace its text.

Updating a paragraph
<p id="message">Waiting for JavaScript...</p>

<script>
  document.getElementById("message").textContent =
    "JavaScript changed this message!";
</script>
Result:
JavaScript changed this message!

document represents the webpage. getElementById("message") finds the paragraph, and textContent changes its text.

The script runs after the paragraph exists, so the browser can find it.

05 / Responding to Clicks

Make a Button Interactive

Websites become interactive when JavaScript responds to events. A click is an event. You can tell a button what to do when that event happens.

A button and its click handler
<button id="helloBtn">Say Hello</button>
<p id="greeting">Waiting...</p>

<script>
  const button = document.getElementById("helloBtn");
  const greeting = document.getElementById("greeting");

  button.addEventListener("click", function() {
    greeting.textContent = "Hello, developer!";
  });
</script>

addEventListener("click", ...) waits for a click. The instructions inside the function run only when the visitor presses the button.

We’ll explore functions and events in depth in later lessons.

06 / Writing Clear Code

Small Habits Make a Difference

Good JavaScript is easier to understand when each instruction has a clear purpose. Use descriptive names, indent related lines, and add comments when they explain something useful.

Readable JavaScript
// Find the welcome message on the page.
const welcomeMessage = document.getElementById("welcome");

// Change its text.
welcomeMessage.textContent = "Welcome to mC0de!";

A line beginning with // is a comment. JavaScript ignores it. Comments help another developer understand why something is happening, but clear names are just as important.

Quick check: What happens if JavaScript tries to find an element before it has been created? The result may be null, and trying to change its text can cause an error. Run your code after the element exists.
07 / Putting It Together

Your First Interactive Page

You’ve seen how to print a message, find an element, and respond to a click. Now bring those skills together.

Start with a heading and a button, then use JavaScript to change the greeting when the button is pressed.

You don’t need to memorize every command. Learn to recognize the pieces, try changing them, and pay attention to what happens.

🚀 mC0de Challenge

Build a Welcome Button

Create a friendly welcome message that appears when someone clicks a button on your page.

Your Mission

  1. Change the heading to welcome visitors to your own website.
  2. Give the button a new label.
  3. Use JavaScript to replace the message when the button is clicked.
  4. Make sure the greeting includes your name or your website’s name.
  5. Run your code and test the button in the live result.
Extra challenge: Change the greeting’s text color when the button is clicked. Can you make it green?
💻 Interactive JavaScript Sandbox

Make Your Greeting Come Alive!

Edit the heading, button, and greeting. Run your code, then click the button in the live result to see what you’ve created.

Live Result
Lesson 01 Recap

What You’ve Learned

You’ve Made the Web Respond!

Your first JavaScript program is a big step. You can now:

  • Explain how JavaScript works alongside HTML and CSS.
  • Print a message using console.log().
  • Add JavaScript to a page with <script>.
  • Find an HTML element and update its text.
  • Make a button respond to a click.

Next up: Variables & Constants — learn how JavaScript remembers and works with information!

Your Learning Journey

0 / 17 Completed

Keep going! Every completed lesson brings you closer to building interactive websites with JavaScript.

🎉

Nice Work!

You've written your first JavaScript program, discovered how JavaScript works alongside HTML and CSS, and learned how to make a webpage respond to a button click. You're officially on your way to building interactive websites!