Output – JavaScript Basics

Output can be displayed in different ways using JS.

Using window.alert( )

This function is used to display a message alert.

<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<script>
window.alert("ALERT");
</script>

</body>
</html>

Output :

Using innerHTML

document.getElementById(id) method can be used in JS to access HTML element. Html element is defined by id attribute and HTML content is defined by innerHTML.

<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<p id="ex"></p>

<script>
document.getElementById("ex").innerHTML = 50 + 50;
</script>

</body>
</html>

Output :

 

 

 

Using document.write( )

<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<script>
document.write(50 + 50);
</script>

</body>
</html>

 

 

 

document.write() after HTML document  will delete all existing HTML content:

<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<button type="button" onclick="document.write(50 + 50)">Click</button>

</body>
</html>

Output :

 

 

 

existing html content is deleted after clicking the button

 

 

 

 

 

 

 

 

Leave a Reply

avatar
  Subscribe  
Notify of