Getting started with JavaScript

  1. Create a new folder on your desktop. Name it something like learning_js.
  2. Open the folder in VS Code. Add a new index.html file.
    Copy this example code into the index.html file.
  3. Create another new file in the learning_js folder and name it script.js.
  4. Add the following line of code at the end of the body of your .html file, just before the closing </body> tag:
    <script src="script.js"></script>
  5. Add the following line of code in the script.js file:
    console.log("hello world!");
  6. Save both files, then open the html file in Firefox or Google Chrome.
  7. Inspect the page (right click anywhere on the webpage and click Inspect, or press Ctrl+Shift+I / Command+Option+I) to open the developer tools.
  8. Click on the Console tab: A screenshot showing the Console tab in browser dev tools
  9. You should see your JavaScript output logged in the console!

A Reddit post that says: 'The absolute best way to learn JavaScript is by building things in JavaScript and learning as you go. You run into a wall you Google it and you get past the problem'

Exercise: log stuff in the console

Once you have attempted the exercises on your own, see sample solutions here.

Basics of JavaScript

JavaScript is a programming language designed for building functional, interactive web pages. It is a widely popular programming language that can do many different things.

Document Object Model (DOM)

The "DOM" is the structure that allows JavaScript code to interact with HTML elements on a webpage. In this model, every element in an HTML document is an "object" that has certain properties and can be acted on via certain methods.

You can use JavaScript code to edit or add functionality to elements on your HTML page using object.method() and object.property syntax. It's probably best to just start by looking at some examples:

  1. Selecting HTML elements:
    // get a single element from its id (this is why ids must be unique!)
    document.getElementById(“myID”);

    // get an array of all the elements in a given class
    document.getElementsByClassName(“myClass”);
    Check for understanding: what is different about the names of these two methods?
  2. Reading HTML text:
    // get the HTML element that holds your page greeting
    let greeting = document.getElementById(“greeting”);

    // console.log(greeting) shows the many properties of this element console.log(greeting);
    Check for understanding: when/why do we write "greeting" in quotation marks vs just greeting as just a word?
  3. Changing HTML elements:
    // update the innerText of your greeting to be “howdy universe”
    greeting.innerText = “howdy universe”;

    // make all elements with class “greenText” have green text
    let greenElements = document.getElementsByClassName(“greenText”);
    for (let i = 0; i < greenElements.length; i++) {
      greenElements[i].style.color = “green”;
    }
    Check for understanding: why do we need a for() loop to change greenElements but not greeting?
  4. Write a function that makes desired changes when an HTML element is clicked:
    // turn all “li” elements red when the function turnRed() is called
    function turnRed() {
    let listItems = document.getElementsByTagName("li");
      for (let i = 0; i < listItems.length; i++) {
        listItems[i].style.color = "red";
      }
    }
    Call your custom turnRed() function in your .html file:
    <!-- call the function inside the tag of the HTML element using onclick="functionName()" -->
    <button onclick="turnRed()">click me!</button>
    Check for understanding: what language is this^?

Exercise: add a button to your webpage that changes a property of HTML element(s). For example, you could try to create: