Different Ways To Display Output In JavaScript

admin_img Posted By Bajarangi soft , Posted On 06-10-2020

JavaScript can "display" data in different ways:Writing into an HTML element, using innerHTML,Writing into the HTML output using document.write(),Writing into an alert box, using window.alert(),Writing into the browser console, using console.log(),To access an HTML element, JavaScript can use the document.getElementById(id) method,The id attribute defines the HTML element. The innerHTML property defines the HTML content.

JavaScript Outputs

1.Writing into an HTML element, using innerHTML.

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My First Paragraph.</p>

<p id="demo"></p>

<script>
    document.getElementById("demo").innerHTML = 5 + 6;
</script>

</body>
</html>
2.Writing into the HTML output using document.write().
<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My first paragraph.</p>

<p>Never call document.write after the document has finished loading.
    It will overwrite the whole document.</p>

<script>
    document.write(5 + 6);
</script>

</body>
</html>
3. Writing into an alert box, using window.alert()
<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My first paragraph.</p>

<script>
    window.alert(5 + 6);
</script>

</body>
</html>

4.Writing into the browser console, using console.log().
<!DOCTYPE html>
<html>
<body>

<h2>Activate Debugging</h2>

<p>F12 on your keybord will activate debugging.</p>
<p>Then select "Console" in the debugger menu.</p>
<p>Then click Run again.</p>

<script>
    console.log(5 + 6);
</script>

</body>
</html> 

Related Post