JavaScript Placement

Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both. In other words, JavaScript can be placed in the <body> and the <head> sections of an HTML page. You can place any number of scripts in an HTML document. A JavaScript function is a block of JavaScript code, that can be executed when "asked" for. For example, a function can be executed when an event occurs, like when the user clicks a button.

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

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

Note: Older examples may use a type attribute: <script type="text/javascript">. This type attribute is not required. JavaScript is the default scripting language in HTML.

Good habit

Keeping all code in one place, is always a good habit.

Back Table of Contents Next