Some of the possible ways we can display output in JavaScript is covered in this tutorial.

console.log()

We use the console.log() method to display the output in the browser console.

How to open browser console?

  • Chrome: View -> Developer -> Developer Tools
  • Firefox: Tools -> Web Developers -> Web Console
  • Safari: Developer -> Show Error Console

Or, simply right click on the page and select Inspect / Inspect Element and then click on the Console tab.

In the following example “Hello World!” will be printed as output in the browser console.

console.log("Hello World!");

JavaScript can “display” data in several ways:

  • An HTML element, using innerHTML.
  • HTML output using document.write().
  • An alert box, using window.alert().
  • Using Inspect Element > browser console, using console.log().

1.Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id) method.

  • The id attribute defines the HTML element.
  • The innerHTML property defines the HTML content.

2.Using document.write()

It is mainly used for testing purposes, document.write().

3.Using window.alert()

window.alert("some message here");

The window.alert() method can be written without the window prefix.

alert(" some message here ");

4.Using console.log()

For debugging purposes, console.log() method is used to display data.

You can get more information about debugging in JavaScript tutorials.


<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My First Paragraph.</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

</body>
</html> 

Output:

My First Web Page

My First Paragraph.

11

Leave a Reply

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