JavaScript Output
JavaScript Output specifies how to display the results of a given code. The result can be shown in four distinct ways, as detailed below:
1. innerHTML:
It is used to access an element. It defines the HTML content.
Syntax
document.getElementById(__id__)
Example
x
<html>
<body>
<h1>Welcome to Webtutor.dev</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "We are here to Build Websites.";
</script>
</body>
</html>
2. document.write():
It is used for testing purposes.
Syntax
document.write(______)
Example
<html>
<body>
<button type="button" onclick="document.write('Webtutor.dev')">Click Me</button><br>
<script>
document.write("Hello World!");
</script>
</body>
</html>
3. window.alert():
It displays the content using an alert box.
Syntax
window.alert(______)
Example
<html>
<body>
<button type="button" onclick="alert('Welcome to Webtutor.dev')">Click Me</button>
</body>
</html>
4. console.log():
It is used for debugging purposes.
Syntax
console.log(______)
Example
<html>
<body>
<p>Note: Please check console</p>
<script>
// Print a simple text message.
console.log("Welcome to Webtutor.dev");
// Print a variable(var) value.
var x = 5;
var y = 10;
var sum = x + y;
console.log(sum);
</script>
</body>
</html>