CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

JavaScript - Hello World

JavaScript - 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 usedocument.write()method. The document.write() method writes the content (a string of text) directly to the HTML document or web page. Lets look at the following βˆ’

JAVASCRIPT CODE
<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.

Preview

Using alert() method

We can use the windowalert()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.

JAVASCRIPT CODE
&lt;script&gt;
   alert("Hello World")
&lt;/script&gt;

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.

Preview

Using console.log()

Theconsole.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 theweb console.

JAVASCRIPT CODE
&lt;script&gt;
   Console.log("Hello World")
&lt;/script&gt;

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.

Preview

It will produce the following message in the web console βˆ’

JAVASCRIPT CODE
Hello World

Using innerHTML

TheinnerHTMLproperty of an HTML element defines the HTML content of the element. We can use this property to display the Hello World message. By changing theinnerHTMLproperty 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”.

Preview