6. Where to place the <script> element?

6.1. Your First JavaScript code


          Let us take a look at our first JavaScript code. The script is placed in the body section of the document and it produces an output that displays a simple text message and a variable value.


  1. <html>
  2. <body>
  3.  <script>
  4.            document.write("Hello World!" + "</br>");

  5.            var score1 = 10;
  6.            var score2 = 20;
  7.            var total = score1 + score2;
  8.            document.write(total);

  9.  </script>
  10. </body>
  11. </html>

          The following result will be provided by the sample code above:



          The console.log() method can also be used to quickly output a message or write data to the browser console.


  1. <script>
  2.            console.log("Hello World!"); /* This prints a simple message: Hello World! */
  3.            var score1 = 10;
  4.            var score2 = 20;
  5.            var total = score1 + score2;
  6.            console.log(total); /* This prints a variable value: 30 */
  7. </script>