Yogesh Chauhan's Blog

4 different Ways to Get JavaScript Output

in JavaScript on July 26, 2019

JavaScript gives us 4 different options to print the output.

  1. innerHTML will write output in HTML element
  2. document.write() will write into HTML output
  3. window.alert() will show output in alert box
  4. console.log() will write output in browser console.

Let’s look at those 4 ways one by one.

1. innerHTML

This is the most common method of displaying data (output).

To access the HTML element we use document.getElementById (name of the id in html) method. We assign ‘id’ to HTML element and innerHTML is the content of that element.

Take a look at the example below.

<body> <h1>.innerHTML Example</h1> <p id="example1"></p> <script> document.getElementById("example1").innerHTML = "innerHTML example"; </script> </body> /* Outptut */ innerHTML example

2. document.write()

<script> document.write("document.write Example"); </script>

The problem with document.write is that if you call it after the document is loaded, then it’ll delete all the existing HTML code and write the new one with the new output.

3. window.alert()

We can use alert box to display output but it’s not useful in all situations. It’s really useful for warnings only. For e.g. if user enters text instead of number. Take a look at the example below…

<body> <button onclick="example3()">Click to get the alert</button> <script> function example3(){ window.alert("You got me!"); } </script> </body>

It will give you an alert when you click on the button.

4. console.log()

We use console.log() mostly for debugging purposes only. You really can’t see the output on your browser screen. Take a look…

<script> console.log("I am console.log Example"); </script>
In Google Chrome, you have to right click on the page and then click on ‘inspect’ and then go to ‘console’ to see the output.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Conditional operator in JavaScript (aka ternary operator)JavaScriptHow to remove a specific item from an array in JavaScript?JavaScript3 Ways we can create URLSearchParams Objects in JavaScriptJavaScriptIN and BETWEEN Operators in SQLSQL/MySQLFile System Integrity: How to Keep an Eye on Your Files and Folder Change?Miscellaneous2 Ways We Can Write Multiple Line Commands in PHPPHP