Output Statements

Displaying output using JavaScript can be done in the following ways:

  1. write
  2. innerHTML
  3. alert

 

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>

 

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>

 

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>

 

 

Comments

Comments used by the developers to write descriptions for various parts of their code.  Comments can be inserted anywhere in the code when and wherever the developer wants.

 

Two styles of comments in javascript:

  1. Single line
  2. Multi line

 

single line

starts with // and ends with current line

 

Multi line

Starts with /* and ends with */.  Everything in between two symbols is considered as non executable part.

You cannot copy content of this page