Javascript -basics of Javascript output

      No Comments on Javascript -basics of Javascript output

What is Javascript?

  • JavaScript is the programming language of HTML and the Web.
  • JavaScript is easy to learn.
  • This tutorial will teach you JavaScript from basic to advanced.

Javascript Output

JavaScript can “display” data in different ways:

  • Writing into an alert box, using window.alert().
  • Writing into the HTML output using document.write().
  • Writing into an HTML element, using innerHTML.
  • Writing into the browser console, using console.log().

 

Window.alert();

Displays an alert box on window.
syntax:

window.alert("your message");


Example:

<script>
function myfun1()
{
window.alert("your message");
}
</script>
<button>Demo</button>


document.write();

Writes message on the document.It should be used only for testing.
syntax:

document.write("i wrote this");

Example:

<script>
function myfun2()
{
document.write("i wrote this");
}
</script>
<button>Demo</button>


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:
syntax:

document.getElementById("demo").innerHTML = "Hi";

Example:

<script>
function myfun3()
{
document.getElementById("demo").innerHTML = "Hi";
}
</script>

Function works on this part

Function works on this part

console.log();

In your browser, you can use the console.log() method to display data.
Activate the browser console with F12, and select “Console” in the menu.
syntax:

console.log(5 + 6);

Leave a Reply

Your email address will not be published. Required fields are marked *