How to display output in javascript?


There are 4 ways to display the output in JavaScript.

 a) Displaying the output in HTML elements, using innerHTML attribute. 

 Live Demo

Example

<html>
<body>
<p id="display"></p>
<script>
   document.getElementById("display").innerHTML = 10 + 10;
</script>
</body>
</html>

Output

20.


b) Displaying the output using document.write().

 Live Demo

Example

<html>
<body>
<script>
   document.write(2 + 3);
</script>
</body>
</html>

Output

5


c) Displaying the output through console.log(). 

 Live Demo

Example

<html>
<body>
<script>
   console.log(2 + 3);
</script>
</body>
</html>

In debugger console, the output

5.


d) Displaying the output through alert box.

 Live Demo

Example

<html>
<body>
<script>
   alert(2 + 3);
</script>
</body>
</html>

In alert window box, the output

5

In case of handling json strings, or some other big data console.log is the best choice.

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements