Where to Place Javascript - jpjohnsonjr/learning-notes GitHub Wiki

In the document

If placed in the script head, would look like this:

<script type="text/javascript"></script>

Could also just use an opening tag and a closing tag and write the javascript in, like so:

<script>
  [javascript entered here]
</script>

In an external file

Would look like this:

<script src="js/script.js"></script>

Note that a script tag always requires a closing tag, even if there is no body within the script tag.

Execution sequence

Like an HTML file, a javascript file is executed sequentially.

Very basic example of declaring a variable

Javascript file:

var x "Hellow World"

HTML file:

<head>
  <meta charset="utf-8">
  <script src="js/script.js"></script>
  <script>
  console.log(x);
  </script>
</head>

The console.log command will log whatever is in the parentheses in the console tab in the developer tools on most browsers. Javascript engine inside the browser is a "single-threaded engine," meaning it will execute completely without being interrupted by any other javascript from the outside.

A <script> tag can also be included in the <body> tag. The effect is that it will be as if you are including it right in the exact part where it appears in the HTML page.

⚠️ **GitHub.com Fallback** ⚠️