Hello World Program in Javascript

In this example, you will learn how to print a simple ‘Hello World’ program in javascript.

I m gonna practically explain to you how you can print “Hello, World!” using 3 different ways.

List of the Ways to Print Hello World

console.log()

alert()

document.write()

1. Using console.log()

console is the window object that has this method log() which helps to print logs on the console. console.log() is generally used for debugging purposes in javascript.

Source Code

console.log(“Hello World!”);

Output

Hello World!

It prints the Hello World to the console.

2. Using alert()

alert() is the window object method. It is used to display an alert box with an OK button.

Source Code

alert(“Hello World!”);

Output

Hello World!

It displays an alert with the message Hello World!

3. Using document.write()

document.write() is a DOM method that directly writes inside HTML.

Source Code

<!DOCTYPE html>
<html>
<body>

<h1>CodingClaw</h1>

<p>Here is the output :</p>

<script>
document.write("Hello World!");
</script>

<p>You successfully printed your output</p>

</body>
</html>

Output

It displays string output Hello World! inside HTML.

Leave a Comment

Your email address will not be published. Required fields are marked *