Friday, November 15, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Course Printing Hello World in JavaScript

JavaScript Course Printing Hello World in JavaScript

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.

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>


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.

    Javascript




    <script>
        // Using console.log
        console.log('Hello World');
    </script>

    
    

    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.

    Javascript




    <script>
        // Using document.write
        document.write('Hello World');
    </script>

    
    

    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.

    Javascript




    <script>
        // Using alert
        alert('Hello World');
    </script>

    
    

    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.

RELATED ARTICLES

Most Popular

Recent Comments