Open In App
Related Articles

JavaScript Course Printing Hello World in JavaScript

Improve Article
Save Article
Like Article

At the start of learning any computer language we tried to print the “Hello World” in that language, here we will do the same and explain the whole code as well.

Whenever we write javascript we make use of the ‘script’ tag. We know what a normal HTML document is made up of The ‘script’ tag is used to write javascript code and this ‘script‘ tag is either placed inside the ‘head‘ tag or inside the ‘body‘ tag.


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, that how to use internal & external JavaScript code.


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

We can put the script tag inside the ‘head’ or ‘body’ tag. Though it should be noted that each choice of putting the ‘script’ tag has its own consequences. For now, you can put the script tag anywhere you want.

Printing Hello World In order to print the famous ‘Hello World’ sentence to the screen, we can make use of different methods that javascript provides. The most common are:

  • Example: Using console.log() method In this example, we will print the legendary “Hello World” in the window console.


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


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

    Console Log Method Output

  • Example: Using document.write() method, in this example, we will print the “Hello World” in the HTML document.


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


    Output:Document Write Method Outpur
  • Example: Using alert() method, in this example, we will print the “Hello World” on the browser window with some message or warning.


        // 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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Mar, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials