Javascript Output
Learn different ways to display the output or result in JavaScript
Published
- Javascript Output
- Example console.log()
- Example window.alert()
- Example innerHTML
- Example document.write()
Javascript Output
Javascript output of a given code can be generated using 4 different ways.
- console.log(): It is used for debugging purposes and output on browser console. Like you want to check value of variable or property.
- window.alert(): It displays the content using an alert box. However, now a days it’s rarely used.
- innerHTML: It is used to access to ready or update an element HTML content. Please note innerHTML use equals sign(=) to assign content.
- document.write(): It is used for testing purpose. It directly output on browser screen.
Example console.log()
<html>
<body>
<h1>webmastercampus.com</h1>
<p> The following code will use for debugging purpose.</p>
<script>
console.log("Sum of 4 + 2 =" + (4+2));
</script>
</body>
</html>
Example window.alert()
<html>
<body>
<h1>webmastercampus.com</h1>
<p> The following code will use for debugging purpose.</p>
<script>
window.alert("Sum of 4 + 2 =" + (4+2));
</script>
</body>
</html>
Read more about window.alert()
Example innerHTML
<html>
<body>
<h1>webmastercampus.com</h1>
<p> The following code will use for debugging purpose.</p>
<div>Result of 4 +2: <span id="result"></span></div>
<script>
document.getElementById("result").innerHTML = 4+2;
</script>
</body>
</html>
Example document.write()
<html>
<body>
<h1>webmastercampus.com</h1>
<p> The following code will use for debugging purpose.</p>
<script>
document.write ("Result of 9 + 2 = "+ (9+2));
</script>
</body>
</html>