This is a very basic article for the beginners, to let the developer know where in the HTML we should place the JavaScript code. While writing the JavaScript as a beginner programmer, most of the time developer doesn’t know where to write the JavaScript code and they spend too much time looking for the solution for it.
JavaScript Code
The JavaScript code is usually enclosed in the script tag in the HTML page. as shown below
<script> var myVar = "Hello World"; alert(myVar); </script>
In the above example I have created a JavaScript code snippet where I have declared a variable named myVar and shown it in the alert which will in turn display a message box when we load the HTML page.
We can place this script based on the need, about when do we want to execute this code. If we want to execute this code before loading the whole of the HTML of the web page in that case this code should be placed in the
section of the HTML page as shown below code.<html lang="en"> <head> <script> var myVar = "Hello World"; alert(myVar); </script> <title>Page Title</title> </head> <body> <p>This is the web page</p>