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 d
ocument.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.