How to use JavaScript


There are several ways to place JavaScript code in our project.


The <script> Tag

In HTML, JavaScript code must be inserted between <script> and </script> tags.


                      <script>
                        document.getElementById("demo").innerHTML = "My First JavaScript";
                    </script>
                  

You can place any number of scripts in an HTML document.

Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.


JavaScript in <head>

In this example, a JavaScript function is placed in the <head> section of an HTML page.

The function is invoked (called) when a button is clicked:


                    <!DOCTYPE html>
                    <html>
                    <head>
                    <script>
                        function myFunction() {
                            document.getElementById("example-1").innerHTML = "Paragraph changed.";
                        }
                    </script>
                    </head>
                    <body>
                    
                    <h1>A Web Page</h1>
                    <p id="example-1">A Paragraph</p>
                    <button type="button" onclick="myFunction()">Try it</button>
                    
                    </body>
                    </html>
                

JavaScript in <body>

In this example, a JavaScript function is placed in the <body> section of an HTML page.

The function is invoked (called) when a button is clicked:


                      <!DOCTYPE html>
                      <html>
                      <body> 
                      
                      <h1>A Web Page</h1>
                      <p id="example-2">A Paragraph</p>
                      <button type="button" onclick="myFunction()">Try it</button>
                      
                      <script>
                        function myFunction() {
                            document.getElementById("example-2").innerHTML = "Paragraph changed.";
                        }
                    </script>
                      
                      </body>
                      </html>
                

Good practices

In most cases, the best place to place JavaScript code is bottom the <body> element.

Placing scripts at the bottom of the <body> element improves the display speed, because script compilation slows down the display.


External JavaScript files

Scripts can also be placed in external files:

External scripts are practical when the same code is used in many different web pages.

JavaScript files have the file extension .js.

To use an external script, put the name of the script file in the src (source) attribute of a <script> tag:


                      <script src="myScript.js"></script>
                    

                    <!DOCTYPE html>
                    <html>
                    <body>
                    
                    <h2>External JavaScript</h2>
                    
                    <p id="example-3">A Paragraph.</p>
                    
                    <button type="button" onclick="myFunction()">Try it</button>
                    
                    <p>(myFunction is stored in an external file called "myScript.js")</p>
                    
                    <script src="myScript.js"></script>
                    
                    </body>
                    </html>
                

Note: And again - it's a good practice to place external JavaScript files at the bottom of the <body> element.


External JavaScript Advantages

Placing scripts in external files has some advantages:

  • It separates HTML and code
  • It makes HTML and JavaScript easier to read and maintain
  • Cached JavaScript files can speed up page loads

To add several script files to one page  - use several script tags:


                      <script src="myScript1.js"></script>
                      <script src="myScript2.js"></script>
                

External References

External scripts can be referenced with a full URL or with a path relative to the current web page.

This example uses a full URL to link to a script:


                      <script src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.6.1/js/mdb.js"></script>
                

This example uses a script located in a specified folder on the current web site:


                            <script src="/js/mdb.js"></script>
                      

This example links to a script located in the same folder as the current page:


                              <script src="mdb.js"></script>
                        

Rate this lesson

Previous lesson Next lesson

Spread the word:
Do you need help? Use our support forum

About the author

User avatar
Michal Szymanski
Co-Founder at MDBootstrap & BrandFlow. Entrepreneur, web developer, UI/UX designer, marketing analyst. Dancer and nerd in one.