First code

The JavaScript code can be written in the ‘html’ file or in the separate ‘JavaScript file (.js)’ as shown in this section.

JavaScript in HTML file

In HTML file, the JavaScript codes can be written inside the ‘script’ tag as shown in Lines 11-13 of below code. The code will write the message “Hello World from JavaScript!” on the web page. Open the ‘js.html’ in the browser to see the message.

 
    	    1 <!-- js.html --> 
2
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>JavaScript</title>
7 </head>
8 <body>
9
10
11 <script type="text/javascript">
12 document.write("Hello World from JavaScript!<br>");
13 </script>
14
15 </body>
16 </html>
JavaScript in Seperate file

The JavaScript code is saved in the file ‘my_javascript.js’ which is located inside the folders ‘asset/js’. Note that, no ‘script tag’ is used here.

  
    	    1 // asset/js/my_javascript.js
2
3 document.write("Hello World from JavaScript!<br>");

Next we need to import the .js file in the HTML file as shown below.

  
    	    1 <!-- js.html --> 
2
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>JavaScript</title>
7 </head>
8 <body>
9
10
11
12 <!-- import JavaScript files here -->
13 <script src="asset/js/my_javascript.js"></script>
14
15 </body>
16 </html>

© Copyright 2025 | FolkLight Studios