JavaScript Program To Print Hello World

A Hello World program, generally known simply as hello world or helloworld, is the traditional first program for any computer language. It is often used in courses and books to illustrate basic concepts of how a programming language works.

JavaScript Program To Print Hello World

In this example we will learn how to print Hello World in multiple different ways.

We will use the following methods to print Hello, World!.

  1. Using console.log()
  2. Using document.write()
  3. Using alert()

1. Using console.log()

A console statement allows you to display a hidden messages to the console. This allows you peak into your code while it executes that can help when troubleshooting bugs. It's a method for developers to write code to inconspicuously inform the developers what the code is doing. It can be used to alert you that there's an issue, but shouldn't take the place of an interactive debugger when it comes to debugging.

It's mostly used for testing and debugging purposes.

console.log('Hello World'); // outputs 'Hello World' to the console

The above code prints the string 'Hello, World!' to the console of your browser.

2. Using document.write()

The document.write() method is a part of HTML Document Object Model (DOM) and there is mainly used for writing some content or JavaScript in an HTML page by replacing the existing content. It is also known as Internet Explorer's proprietary technology.

With the new release of HTML5, document.write() method is made even more useful. It allows you to write content to the HTML page without having to use a script tag.

However, it is not recommended to use document.write() method for writing content to the HTML page. It is better to use the innerHTML property to add/modify the contents of an HTML page.

document.write('Hello, World!'); // prints 'Hello, World!' to the page

3. Using alert()

The alert() method is used to display a message in a pop-up window. It is usually used to display a message to the user when the user clicks on a button.

The alert method forces the user to read the message. It does this by taking the focus away from the current window, and displaying a modal dialog which is covered by the alert itself until it is dismissed by the user.

While JavaScript alert is commonly used as a quick and simple way to provide information to the user, it should be avoided.

alert('Hello, World!'); // opens a pop up with the message "Hello, World!"

Summary

In this tutorial, we have learnt the basics of JavaScript programming. We have learnt how to print the string 'Hello, World!' to the console using console statements, document.write(), and alert() methods. We have also learnt how to use the innerHTML property to add/modify the contents of an HTML page.

Please checkout our related content section below for more tutorials on similar topics.


We hope that you have learnt something from our tutorials. Please feel free to share this content with your friends.


If you have any questions or want some other content to be added to our website so that it can help you learn and grow further, please do not hesitate to contact us at info@createmysite.org. We would love to hear from you.

We are hiring!! If you are tech blogger who loves writing content on modern technologies, we would love to get in touch with you. Just drop us a mail at the email address mentioned above and we will reach out to you as soon as possible.

If you are interested in learning more about our website, please visit our about page.