How to display output in javascript?

JavascriptObject Oriented ProgrammingProgramming

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.

raja
Published on 21-May-2019 18:27:57
Advertisements