1. Javascript code can be written directly in html pages (can be in head or body tags)

Example:

     <html>
      <head>
       <script language="javascript" type="text/javascript">
       //Javascript code can be written in head. 
       </script>
      </head>
       <body>
         <script language="javascript" type="text/javascript">
          //Javascript code can be written in body
         </script>
       </body>
     </html>
2. We can write javascript code in external javascript file. External javascript file must have .js extension. a. Create an external javascript file with the name sample.js and write the following code as shown below.
function fnSayHello() {
alert('i am from external js file');
}
b. Link html and external javascript files using following code (You can include this code either in head or body tags of an html/aspx file).
<script type="text/javascript" src="Sample.js"></script>
Note: In external javascript file the following script tag is not required and is not allowed.
<script language="javascript" type="text/javascript"></script>
Note: We can achieve re-usability of javascript code by writing javascript functions in external javascript files.