JavaScript Output
DAY 2

JavaScript Output

What is Javascript Output?

JavaScript Output?defines the ways to display the output of a given code. The process of communicating with the outside world?is known as input/output or I/O. Watson JavaScript includes two output statements for displaying information on the computer's screen, and two input statements – one for reading in the text input, the other for reading in numeric input.

There are certain situations in which you may need to generate output from your JavaScript code. For example, you might want to see the value of variables or write a message to the browser console to help you debug an issue in your running JavaScript code, and so on.

In JavaScript there are several different ways of generating output including writing output to the browser window or browser console, displaying output in dialog boxes, writing output into an HTML element, etc.

JavaScript can "display" data in different ways. Thus;

  • 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().

A. Using innerHTML

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. See below.

<!DOCTYPE?html
<html>
<body>

<h1>My First Web Page</h1>
<p>My First Paragraph</p>

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

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

</body>
</html>        

B. Using document.write()

For testing purposes, it is convenient to use?document.write():

N/B

Using document.write() after an HTML document is loaded, will?delete all existing HTML

<!DOCTYPE?html
<html>
<body>

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

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

</body>
</html>

        

C. Using window.alert()

You can use an alert box to display data:

<!DOCTYPE?html
<html>
<body>

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

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

</body>
</html>        

D.Using console.log()

For debugging purposes, you can call the?console.log()?method in the browser to display data.

<!DOCTYPE?html
<html>
<body>

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

</body>
</html>        

Here, in this article, I try to explain?JavaScript Output?with examples based on What I learned today (See the published date). I hope this article helps you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

要查看或添加评论,请登录

Solomon Iniodu的更多文章

社区洞察

其他会员也浏览了