Computer >> Computer tutorials >  >> Programming >> Javascript

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. 

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().

Example

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

Output

5


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

Example

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

In debugger console, the output

5.


d) Displaying the output through alert box.

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.