Writing your First JavaScript Program

Learn how to create your first JavaScript application with this beginner-friendly tutorial. Build a simple program to display a message to the user and start exploring the possibilities of this versatile language.

Updated: March 11, 2023


This YouTube channel is packed with awesome videos for coders!

Tons of videos teaching JavaScript and coding. Cool live streams!

Click Here to check out the Channel!

Welcome to your first JavaScript application tutorial! In this guide, we’ll create a simple program to display a message to the user. First, create a new HTML file and add the following code to it:

<!DOCTYPE html>
<html>
  <head>
    <title>My First JavaScript Application</title>
  </head>
  <body>
    <h1 id="message"></h1>
    <script src="app.js"></script>
  </body>
</html>

This code creates a new HTML document with a header tag that we’ll use to display our message. We’ve also added a script tag referencing a JavaScript file called “app.js”.

Next, create a new file called “app.js” and add the following code to it:

var message = "Hello, world!";
document.getElementById("message").innerHTML = message;

This code creates a new variable called “message” and sets its value to “Hello, world!”. It then uses the “getElementById” function to select the “message” header tag in the HTML document and sets its “innerHTML” property to the value of the “message” variable.

Let’s test it out!

Save both files and open the HTML document in your web browser. The message “Hello, world!” should be displayed in the header tag. Congratulations! You’ve just created your first JavaScript application. This is just the beginning - JavaScript is a powerful language that can be used to create all sorts of dynamic and interactive web applications.

New Stuff to Try

To continue learning, try experimenting with different types of variables and functions. You could create a program that prompts the user for their name and displays a personalized message, or you could create a program that performs a calculation based on user input. The possibilities are endless.

Conclusion

Creating your first JavaScript application is an exciting and rewarding experience. With just a few lines of code, you can create a program that interacts with the user and displays dynamic content on a web page. So keep practicing and exploring the world of JavaScript programming - the sky’s the limit!