Completion requirements
View
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.
- <html>
- <body>
- <script>
- document.write("Hello World!" + "</br>");
- var score1 = 10;
- var score2 = 20;
- var total = score1 + score2;
- document.write(total);
- </script>
- </body>
- </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.
- <script>
- console.log("Hello World!"); /* This prints a simple message: Hello World! */
- var score1 = 10;
- var score2 = 20;
- var total = score1 + score2;
- console.log(total); /* This prints a variable value: 30 */
- </script>