JavaScript Tutorials

Overview

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 -->
<script src="/scripts/main.js"></script>

</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 -->
<script type="text/javascript" src="/scripts/main.js"></script>

</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 -->
<script>
    function myFunction() {
        var element = document.getElementById('myDIV');
        element.classList.add('mystyle');
    }
</script>

</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 -->
<script>
    function myFunction() {
        var element = document.getElementById('myDIV');
        element.classList.add('mystyle');
    }
</script>
<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>

<script>
function myFunction() {
    document.getElementById('my_output').innerHTML = 'New content';
}
</script>

JavaScript Notes:

  • When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • JavaScript is a case-sensitive language; firstName is NOT the same as firstname
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
  • JavaScript variables must begin with a letter, $, or _
  • JavaScript variables are case sensitive (x is not the same as X)

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.