JavaScript Hello World: Write a Simple Program in 3 Ways

If you are ready to take your first steps into the realm of web development, then there cannot be a better way other than writing a ‘JavaScript Hello World’ program as shown below.

This article will walk you through the process of building the classic “Hello World!” in JavaScript program.

So, let’s see different ways to write a —”Hello World” program in Javascript:-

  • Display the ‘Hello World’ program through the browser console
  • Show the “hello world” inside the alert popup box.
  • Display the “hello world” within the HTML DOM element.

Before we get started let’s set up our workspace for JavaScript.

Setting Up Your Environment

You only need a text editor and a web browser to begin. Open your preferred text editor—whether it’s Visual Studio Code, Sublime Text, or another—and create a new file. Give it a name like helloWorld.html to indicate it’s an HTML file.

Start writing your code according to the structure you need, whether it is embedded in JavaScript or an external file.

In this example, we will use embedded JavaScript code within HTML.

<!-- helloWorld.html -->
<!DOCTYPE html>
<html>
  <head>
      
      <title>Hello World Program</title>
      
  </head>
  <body>
      
      <h1>Welcome to JavaScript Hello World program</h1> 
      
      <!-- The embedded JavaScript code would be here -->
      <script type="text/javascript">
        // => Your JavaScript Here
      </script>
      
  </body>
</html>

According to the above HTML structure, we will write our JavaScript code inside the <script> tag.

Running JavaScript in the Browser: Print “Hello World” in Browser Console

In this program, we will use the console.log function to print the text in the browser console.

Here is the code of JS.

<!-- helloWorld.html -->
<!DOCTYPE html>
<html>
  <head>
      
      <title>Hello World Program</title>
      
  </head>
  <body>
      
      <h1>Welcome to JavaScript Hello World program</h1> 
      
      <!-- The embedded JavaScript code would be here -->
      <script type="text/javascript">
        console.log('Hello, World!');
      </script>
      
  </body>
</html>

Now, let’s open the browser console by doing the following steps:-

  1. Right-click on the web page.
  2. Select the “inspect” object from the menu.
  3. Navigate to the “Console” tab.
  4. The output of this program should be printed in the browser console like in the below image.
JavaScript hello world

We can also display the same output using JavaScript’s built-in alert function. This is done through a pop-up box that appears on the webpage during actions.

So, let’s see how can we do that in the following section.

Displaying the Message Using the Alert Function in JavaScript

Basically, JavaScript provides us with a simple notification or message to the user while exploring the browser—It is aleret().

Here is an example of its syntax.

alert('Hello, World!');

Let’s see what happens if we add this line of code to our webpage.

<!-- helloWorld.html -->
<!DOCTYPE html>
<html>
  <head>
      
      <title>Hello World Program</title>
      
  </head>
  <body>
      
      <h1>Welcome to JavaScript Hello World program</h1> 
      
      <!-- The embedded JavaScript code would be here -->
      <script type="text/javascript">
        alert('Hello, World!');
      </script>
      
  </body>
</html>

So, when the webpage is loaded the JavaScript will trigger a pop-up on the browser containing a message of — “Hello, World” as a dialog box.

JavaScript alert

Additionally, there is another way to show — the “Hello, World” text on the webpage using the document.write. Let’s move on to the following part to understand how can we do that.

Using document.write to Print — the “Hello World” in JavaScript

Actually, document.write is a built-in method in JavaScript that allows us to write content directly to the webpage.

This function is executed when the browser starts parsing the HTML. Here is an example.

<script type="text/javascript">
    document.write('Hello, World!');
</script>

Similar Reads

JavaScript Math round(): How It Works With Examples

JavaScript gives you the Math.round() function to deal with decimal numbers. You use it when you want to round a…

JavaScript for Loop: How to Iterate Arrays by Examples

JavaScript runs code in different ways, but the for loop stays one of the most common tools. You use it…

JavaScript Math abs(): How It Works with Examples

Some numbers come from user input and other come from math operations. Sometimes, these numbers need to stay positive. The…

Understanding Comments in JavaScript for Beginners

Comments in JavaScript help you explain code and prevent mixing. They guide anyone who reads or edits your script. Understand…

Flatten an Array in JavaScript: Use flat() to Handle Nested Arrays

Arrays often hold other arrays. This happens with API responses, form data, or nested objects. These layers add extra steps…

JavaScript valueOf Function: How it Works with Examples

You will learn what the JavaScript valueOf function does and how it works. It returns the primitive value of an…

JavaScript Math tan: The Tangent of an Angle in Radians

JavaScript Math.tan() finds the tangent of an angle in radians. You use it when you need to solve angle-based math.…

JavaScript “use strict” Mode: What It Is and Why You Should Use It

Code mistakes used to slip by without a warning. That made bugs hard to trace and fix. JavaScript "use strict"…

JavaScript Functions: How to Create Functions in JavaScript

In this guide, you will learn how JavaScript functions work and why they matter. Each section shows what you need…

JavaScript Math ceil: Round Numbers with Precision

Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…

Previous Article

JavaScript Syntax: The Complete Cheat Sheet

Next Article

JavaScript: How to Add JS to HTML

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.