Open In App

JavaScript Hello World

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Whenever we start to learn any language we prefer to write or print “Hello World” in the beginning. So, here we also going to see how can we print “Hello World” using JavaScript. To print “Hello World” on the screen we need to use HTML also, and to do that we have to use a script tag in the HTML document.

Script tag enables the use of JavaScript in the HTML file and it can be inside of the head tag or body tag depending on when we want to load our JavaScript file onto the browser.

Example: In this example, we will not include any JavaScript code, here we are just putting the script tag in the HTML code to show you, how to use internal & external JavaScript code.

HTML




<!DOCTYPE HTML>
<html>
  
<head>
    <title>JavaScript Course - Hello World</title>
    <!-- Script tag can also be placed here -->
    <script src="main.js">
       // External JavaScript Code
    </script>
</head>
  
<body>
    <p>Happy Learning of JavaScript</p>
    <script>
        // Internal JavaScript Code
    </script>
</body>
  
</html>


The most common methods of printing “Hello World” are:

Using console.log() method

Example: In this example, we will print the legendary “Hello World” in the window console.

Javascript




// Using console.log
console.log('Hello World');


Output: Press Ctrl+Shift+J to see the output in the browser console.

Console Log Method Output

Using document.write() method

Example: In this example, we will print the “Hello World” in the HTML document.

Javascript




// Using document.write
document.write('Hello World');


Output:

Document Write Method Outpur

Using alert() method

Example: In this example, we will print the “Hello World” on the browser window with some message or warning.

Javascript




// Using alert
alert('Hello World');


Output:

Alert Method Output

Each of the above methods has different ways of outputting the content. Though ‘document.write()’ is used when we want to print the content onto the document which is the HTML Document. Also ‘console.log()’ is mainly used when we are debugging JavaScript code and the ‘alert()’ is used to show an alert box on the browser window with some message or warning.



Last Updated : 08 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads