JavaScript Output

Javascript does not have any built in capabilities to print or display. It uses browser’s window and document object for display or print.

Display Possibilities using Javascript

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

window.alert()

Alert box is used to display data to user in separate modal dialog. You need to press OK to continue.

Example

<html>
	<body>
		<h1>My Web Page</h1>
		<p >My Paragraph</p>
		
		<script>
		window.alert(5 + 6);
		</script>
	</body>
</html>

Download the code Run the code

document.write()

It displays output in the document itself. It is generally used for testing and banners

<html>
	<body>
		<h1>My Web Page</h1>
		<p >My Paragraph</p>
		
		<script>
		document.write(5 + 6);
		</script>
	</body>
</html>

Download the code Run the code

console.log()

All modern browsers come with a in-built console. console.log() method is used to display data in console. F12 key in browser opens the console. Console is very useful in debugging and testing JavaScript code. In Console pane there is a box at right side where you can write any JavaScript code and execute it without reloading a page.

Firebug is a debugging tool which uses Browser’s console. console.log() is used to print information messages and JavaScript errors. JavaScript syntax errors are shown in Error Console window.

<html>
	<body>
		<h1>My Web Page</h1>
		<p >My Paragraph</p>
		
		<script>
		console.log(5 + 6);
		</script>
	</body>
</html>

Download the code Run the code

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .