Where JS code

  • Javascript code can be written inside script tags.
  • Those look just like usual HTML elements, and they can be placed in the head or the body section.
  • For most cases, the best place to include the script tags will be at the body element’s end.

    <!DOCTYPE HTML>
    
    <html>

        <head>
            <script>

                // Javascript code can be written here

            </script>
        </head>
        
        <body>


            <!-- page content  -->


            <script>

                // Javascript code can be written here

            </script>

        </body>
    </html>
                    
  • However, the best place to write Javascript code is in separate .js files.
  • In this case, the script tag will serve as a link to that file.

    <body>


        <!-- html -->


        <script src='js/scripts.js'></script>
        

    </body>
                    

Writing the first Javascript statements

  • The message passed through the alert function is shown on a popup

 


    alert('Sending a message via alert');


  • The message sent through the console.log function can be seen on the browser’s inspector.


    console.log('Sending a message via the console');
Scroll to Top