JavaScript can be used to change data on the page, rewrite the entire page, create popup alerts, write to the console for testing, or print the page.

Writing Content to the Page

The Element innerHTML property can be used to set the HTML content of an element. The HTML element is selected by its id #my_output, and the innerHTML (text inside the paragraph element) is changed to “New content”.

				
					<!-- output to the HTML element -->
<p id="my_output">My content</p>
				
			
				
					document.getElementById('my_output').innerHTML = 'New content';
				
			

Re-Writing the Page

The document.write() method writes directly to an open HTML document. The document.write() method deletes all existing HTML when used on a loaded document.

				
					document.write('Hello World!');
				
			

Creating an Alert Popup in the Browser

The Window alert() method displays an alert popup in the browser with a message and OK button, and is used to display information to the user.

				
					alert('Hello World');
				
			

Writing to the Browser Console for Testing

The console.log() method logs a message to the browser console, which is useful for testing purposes.

				
					console.log('Hello world');
				
			

Printing the Page

The window.print() method opens a print dialog box, which lets the user select from printing options to print the contents of the window.

				
					window.print();