javascript example programs code

Before starting an example let us discuss how JavaScript can display the data.

JavaScript ways of data display:

1. window.alert(): Display data into an alert box.
2. document.write(): Display data into the HTML output.
3. innerHTML: Display data into an HTML element.
4. console.log(): Display data into the browser console.

Javascript in head tag example:

<html>  
<head>  
<script type="text/javascript">  
  alert("Hello tutorialspointexamples.com");  
</script>  
</head>   
</html>

Try it:

JS Bin on jsbin.com

Javascript in body tag example:

<html>  
<body>  
<script type="text/javascript">  
  alert("Hello tutorialspointexamples.com");  
</script>  
</body>   
</html>

Try it:

JS Bin on jsbin.com

Javascript in head and body tag example:

<html>  
<head>  
<script type="text/javascript">  
function sayHello(){  
 alert("Hello tutorialspointexamples.com");
}  
</script>  
</head>  
<body>  
<p>Hello World Javascript Example.</p>  
<form>  
<input type="button" value="Say Hello" onclick="sayHello()"/>  
</form>  
</body>  
</html>

Try it:

JS Bin on jsbin.com