Hello World Program

Write “Hello World” Program in JavaScript

“Hello, World!” is often the first program, programmers write when learning a new programming language. JavaScript “Hello World” is a simple program, generally used to demonstrate the basic syntax of the language. This program will make use of different JavaScript methods to print “Hello World”.

Using document.write()

In JavaScript, the simplest way to print “Hello World” is to use document.write() method. The document.write() method writes the content (a string of text) directly to the HTML document or web page. Let’s look at the following −

<script>
   document.write("Hello World)</script>

We should write the document.write() within the <script> and </script> tags. We can place the <script> inside the <head> or <body> section.

Example

Let’s try to write a JavaScript program that prints the “Hello World!” to the document or web page. In the below program, we placed the JavaScript code within the <head> section. You can try to put the JavaScript part inside the <body> section and execute the program.

<html><head><script>
      document.write("Hello World");</script></head><body></body><html>

Using alert() method

We can use the window alert() method to print “Hello Word” in a dialogue box.

The alert() is a window method that instruct the browser to display an alert box with the message. We can write alert without passing any message to it.

<script>alert("Hello World")</script>

We can write alert() method as window.alert(). As window object is a global scope object, we can skip the “window” keyword.

Example

Let’s try an example showing “Hello World” in the alert box. Try to execute the program by putting the <script> part within the <body> section.

<html><head><script>alert("Hello World");</script></head><body></body><html>

Using console.log()

The console.log() is a very convenient method to print the message to web Console. It is very useful to debug the JavaScript codes in the browsers. Let’s look at the simple application of the console.log() to print the “Hello World” to the web console.

<script>
   Console.log("Hello World")</script>

We can use the console.log to print strings or JavaScript objects. Here in above code snippet, we have passed “Hello World” as a string argument to the console.log() method.

Example

Let’s try to write a complete JavaScript program with HTML.

<html><head><script>
      console.log("Hello World");</script></head><body><p> Please open the console before clicking "Edit & Run" button </p></body><html>

It will produce the following message in the web console −

Hello World

Using innerHTML

The innerHTML property of an HTML element defines the HTML content of the element. We can use this property to display the Hello World message. By changing the innerHTML property of the element, we can display the message in HTML document or webpage.

To use innerHTML property of an element, we need to access that element first. We can use document.getElementById() method to access the element. Let’s look at a complete example.

Example

In the example below, we define a div element with id, “output”. We access this element using the document.getElementById(“output”). Then we change the innerHTML property and display our message, “Hello World”.

<html><head><title>Using innerHTML property</title></head><body><div id ="output"></div><script>
      document.getElementById("output").innerHTML ="Hello World";</script></body><html>

Comments

Leave a Reply

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