JavaScript is one of the three main languages (along with HTML and CSS) for coding a website.

While HTML is the page structure, and CSS the page design, JavaScript can be considered the page “action”, as it is often used to interact with the user.

Example of Using JavaScript

				
					<!-- html element to place output -->
<p id="my_output"></p>
				
			
				
					// place text in the #my_output element
document.getElementById('my_output').innerHTML = 'Hello World';
				
			

Note
Although JavaScript is a programming language, it’s built into every browser, and there is no need to download/install anything extra to use it.
Note
When using JavaScript, single or double quotation marks are acceptable and work identically to one another. Choose whichever you prefer, and stay consistent. However, if you are writing a string to the page that contains either a double or single quote, you will need to either use the opposite set, or place a backslash prior to the quote.

document.getElementById('my_output').innerHTML = "Johnny's World";
or
document.getElementById('my_output').innerHTML = 'Johnny/'s World';

Linking to an External Document

An external JavaScript file (or multiple files) are used to define the various site actions, while keeping everything centralized. This is a best practice because if you were to place your scripts directly in the head of each document (or scattered throughout the body), there would be no centralized location to make your changes and you’d have to search for all the various rules across multiple locations.

The following shows the head of our document, and how the link should be placed.

				
					<head>
<title>Advanced HTML Template</title>
<!-- styles -->
<!-- scripts -->
</head>
				
			

Non-crucial scripts can be placed just before the closing /body tag which can speed up the page load time because the page can first render before applying the scripts.

				
					<!-- scripts -->
</body>
</html>
				
			

Placing Scripts Directly on the Page

Scripts can be placed directly in the head of the document or even inline with the element they are modifying, but neither are good practice.

However, if you must place them in the document itself, the following shows where we want to place our script(s). Typically this is done directly after the title tag.

				
					<head>
<title>Advanced HTML Template</title>
<!-- styles -->
<!-- scripts -->
</head>

				
			

Scripts can also be placed just before the closing /body tag which can speed up the page load time because the page can first render before applying the scripts.

				
					<!-- scripts -->
<noscript>Sorry, your browser does not support JavaScript!</noscript>
</body>
</html>
				
			

Note
Technically, a script can be placed anywhere in the body of the page. If doing so, it is good practice to place a noscript tag just after the script tag for browsers that cannot run JavaScript for some reason.

Inline Scripting

Another way to place styles (again, not the best way to do it!), is to place them directly with the element they are interacting with.

The following has a call to the JavaScript right within the button element. And the script is just below it.

Again, this is not a great practice and makes it difficult to locate the scripting and make changes, especially if it occurs on multiple pages.

				
					<!-- html element to place output -->
<p id="my_output">My content</p>
<button type="button" onclick="myFunction()">Change Text</button>