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.
const message = "Hello, JavaScript!";
console.log(message);
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.
Click the button below to see JavaScript in action.
Click the button to reveal a greeting.
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.
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.
Give Your Page Instructions
JavaScript can be placed inside an HTML <script> element. When the browser reaches that element, it runs the code.
<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.
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.
<p id="message">Waiting for JavaScript...</p>
<script>
document.getElementById("message").textContent =
"JavaScript changed this message!";
</script>
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.
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.
<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.
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.
// 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.
null, and trying to change its text can cause an error. Run your code after the element exists.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.
Build a Welcome Button
Create a friendly welcome message that appears when someone clicks a button on your page.
Your Mission
- Change the heading to welcome visitors to your own website.
- Give the button a new label.
- Use JavaScript to replace the message when the button is clicked.
- Make sure the greeting includes your name or your website’s name.
- Run your code and test the button in the live result.
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.
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 CompletedKeep 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!


