To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Part 2.3Basic output methods in JavaScript

So far this tutorial has relied on just two methods of output; console.log and console.error. JavaScript provides at least four methods of output. This part of the tutorial will describe two additional methods of output.

document.write

The function used thus far have all been highly unobtrusive and do not do anything but allow debugging. The document.write is another very useful debugging output method.

JavaScript
document.write("Hello world!")

Another form of this is the is document.writeln which will write a line to the document:

JavaScript
document.writeln("Hello world!")

Using the document.write and the document.writeln functions will overwrite any existing HTML in the document. and should only be used for debugging purposes.

window.alert

The most useful output when developing a usable web document that JavaScript provides is the window.alert function.

JavaScript
window.alert("Hello world!")

The window.alert function is a formalised version of the alert function:

JavaScript
alert("Hello world!")
Code previewClose
Feedback 👍
Comments are sent via email to me.