In this post, you will learn how to print a simple Hello World or output of any program in javascript.
I m gonna practically explain to you how you can print anything using 3 different ways. So the ways that I m going to explain to you can help you to print strings, numbers, and boolean.
List of the Ways to Print in Javascript
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.
Printing String using console.log()
console.log(“Hello World!”);
Output
Hello World!
It prints the Hello World
to the console.
Printing Integer using console.log()
console.log(100);
Output
100
It prints the integer 100
to the console.
We can also evaluate the expression and print its output.
console.log(5*5+10);
Output
35
It will print 35
;
Printing Boolean using console.log()
console.log(10>1);
Output
True
It prints the boolean true
to the console.
2. Using alert()
alert()
is the window object method. It is used to display an alert box with an OK button.
Printing String using alert()
alert(“Hello World!”);
Output
Hello World!
It displays an alert with the message Hello World!
Printing Integer using alert()
alert(100);
Output
100
It displays the integer 100
in the alert box
We can also evaluate the expression and display the output in the alert box.
alert(5*5+10);
Output
35
It will display 35
;
Printing Boolean using alert()
alert(10>1);
Output
True
It displays the boolean true
in the alert box.
3. Using document.write()
document.write()
is a DOM method that directly writes inside HTML.
Printing Integer using document.write()
<!DOCTYPE html>
<html>
<body>
<h1>CodingClaw</h1>
<p>Here is the output :</p>
<script>
document.write(100);
</script>
<p>You successfully printed your output</p>
</body>
</html>
Output

It displays integer output 100
inside HTML.
We can also evaluate the expression and display the output inside HTML.
<!DOCTYPE html>
<html>
<body>
<h1>CodingClaw</h1>
<p>Here is the output :</p>
<script>
document.write(5*5+10);
</script>
<p>You successfully printed your output</p>
</body>
</html>
Output

it will evaluate and display the output inside HTML.
Printing String using document.write()
<!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.
Printing Boolean using document.write()
<!DOCTYPE html>
<html>
<body>
<h1>CodingClaw</h1>
<p>Here is the output :</p>
<script>
document.write(10>2);
</script>
<p>You successfully printed your output</p>
</body>
</html>
Output

It displays boolean output true
inside HTML.
So now you know how to print in javascript. We have shown you 3 main ways to print the output of the javascript program. You can use these ways to print the output of your javascript programs.