JavaScript Examples

The JavaScript example is the easiest to code. The reason being there are three places where the code can be placed

Here are the three places where the JavaScript can be saved.

One is within the existing body tag, it can be saved in the head tag and last but not the least it can be saved in the external JavaScript file.

The script tag present in the JavaScript file mentions that we are using the JavaScript.

Next the text or the actual script used this is the content which will provide information to the browser for the data. The document.write() is the function which is used in order to display the dynamic content using the JavaScript.


There are three main places where the JavaScript code can be embedded are mentioned below:

  • Between the tag of the body of the text
  • Between the head tag
  • In the .js file

Between the tag of the body of the text


<script type="text/javascript">
 alert("Hi javatportal");
</script>
 

Between the head tag

 <html>
<head>
<script type="text/javascript">
function msg(){
 alert("Hi javatportal");
}
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>