You will come across different situations where you need to see the output for your JavaScript code.
For instance: You need to check the variable value.
JavaScript offers different methods to see the outputs which are discussed below.
Use the console.log( ) method to write messages to the browser console. Press the F12 key to open the developers tools. Click on the Console tab to access the web browser console.
console.log("Introduction to JavaScript!");
var a = 10;
var b = 15;
var total = a + b;
console.log(total);
Use the alert( ) method to create an alert dialog box that showcases the output data.
alert("Introduction to JavaScript");
var a = 10;
var b = 15;
var total = a + b;
alert(total);
Use the document.write( ) method to write data to the current document when it is being parsed.
document.write("Introduction to JavaScript!");
var a = 10;
var b = 15;
var total = a + b;
document.write(total);
The innerHTML property of an element is used to write or insert output in the HTML element. However, you need to choose the element using the getElementByID( ) method in advance of writing the output.
<p id="show"></p>
<p id="output"></p>
<script>
document.getElementById("show").innerHTML = "Introduction to JavaScript";
var a = 10;
var b = 15;
var total = a + b;
document.getElementById("output").innerHTML = total;
</script>