Computer >> Computer tutorials >  >> Programming >> Javascript

How to include JavaScript in HTML Documents?


To include JavaScript in HTML Documents, use the <script> tag. JavaScript can be implemented using JavaScript statements that are placed within the <script>... </script>.

You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags.

Let us take a sample example to print out "Hello World". We added an optional HTML comment that surrounds our JavaScript code. This is to save our code from a browser that does not support JavaScript. The comment ends with a "//-->".

Here "//" signifies a comment in JavaScript, so we add that to prevent a browser from reading the end of the HTML comment as a piece of JavaScript code. Next, we call a function document.write, which writes a string into our HTML document.

Live Demo

<html>
   <body>
      <script>
         <!--
            document.write("Tutorialspoint")
         //-->
      </script>
   </body>
</html>