In JavaScript we have multiple way by using which we can display the output of our program. JavaScript can “display” data output in different ways, let’s discuss all below.

  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().
  • Writing into an alert box, using window.alert().
  • Writing into the browser console, using console.log().

Use of innerHTML

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

The id attribute identifies the HTML element. The InnerHTML property used to set or get the data from the given HTML element.

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

Use of document.write()

This is used to write the content on page, for debugging or for testing purposes, it is convenient to use document.write() with in your JavaScript program.

NOTE: Be sure it will delete all existing HTML form your page and write the only content which you write on this page.

<script>
   document.write(4 + 6);
</script>

Use of window.alert()

This can be used to show alert box to display data. You can use this while debugging or when you explicitly need to show data in the form of ALERT. You can use either window.alert or alert both will work in same way.

<script>
  window.alert(5 + 6);
</script>

Use of console.log()

This can be used for debugging purposes, you can call the console.log() method in the browser to display data. It is very helpful to read debugging data and do the trouble shooting.

<script>
   console.log(5 + 6);
</script>

Apart from above window.print() is there which is used to print the browser content. You will not be able to access output devices but yes can use this method in the browser to print the content.

Leave a Reply

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

Explore More

How to Center a Popup Window on Screen

JavaScript window.open() method is used to open a popup window. This popup window will be placed in the center of the screen. This example creates the pop-up window without placing it into

How to hide URL in the popup window opened using window.open

If you are using below code to open a popup window in your web page, then address must be appearing in you pop up window and f you want to

window.showModalDialog is deprecated in Edge and Chrome

If you have a website being compatible with Edge/Chrome and in past it was only compatible with IE 11 and if you are using window.showModalDailog in your JavaScript code, then