Javascript Hello, World Program!

To print out Hello, World! using Javascript you can use one or more ways. Javascript comes with four different ways for generating output to the display including window popup output.

We will learn more about Javascript output in the later chapters.

We can use both inline Javascript or external Javascript for Hello, World program. In this tutorial, we will use internal or inline Javascript.

Hello, World Example(s):

Example-1:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, World Program!</title>
    </head>
    <body>

        <h2 id="ex1"></h2>

        <script>
            document.getElementById("ex1").innerHTML = ("Hello, World!");
        </script>

    </body>
</html>

Output:

Hello, World!

In the above example, We have used getElementById method to target the id for placing the text using innerHTML calling method.

We will learn every single method and calling functions in the later chapters. For now, don’t worry about this.

Example-2:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, World Program!</title>
    </head>
    <body>

        <h2></h2>

        <script>
            var myHeading;
            myHeading = document.querySelector('h2');
            myHeading.textContent = 'Hello world!';
        </script>

    </body>
</html>

Output:

Hello, World!

In this example, we have declared a variable called myHeading. Then we have assigned the targeting value to the variable. we have used the querySelector() function for targeting HTML tag and the var.textContent function for placing the text.

« Previous Next »