JavaScript

JavaScript is a programming language that is used on many websites to make them respond to people's interactions. Along with HTML and CSS, JavaScript is one of the most important tools for making websites.

You can see the source code that makes a website by right-clicking in your browser and choosing "Inspect". The code that appears in the "Elements" tab is what you are seeing in your web browser. Try right-clicking on this text.

While you are there, see what else is makes this page look the way it does. Can you find the Holyoke Codes logo?

You can run Javascript in the browser "Console". Try typing in the console:

console.log("Hello world!");

You can also do math with JavaScript if you need a handy calculator. Try:

5+5

or

10 * 5 / 12

Functions

Functions are ways to name code and make it re-usable. Here's an example:

function sum() { return 10 + 10 }

If you type this into your console, you can run this function with the command

sum()

If you want to include JavaScript in a webpage you can use the <script> HTML tag. See if you can figure out what this program does:

<button onclick="getNumbers()">Add numbers</button>

<script>
    let total = 0;
    function getNumbers() {
        n1 = Number( prompt("Enter first number:") );
        n2 = Number( prompt("Enter second number:") );
        sum();
        alert("The total is " + total);
    }
    function sum() { total = n1 + n2 }
</script>

Now try it out by clicking the button.

Hint

In the briefcase challenge, you will need to fix a function that does not do what it is supposed to do. You can save time if you copy the function from the source code and paste it into the console. If you are inspecting the <script> tag, try right-clicking on the JavaScript code and select "Edit text". Then you can copy and paste the code you want to modify. Once you have pasted it into the console, you can make your changes and hit "enter" to run your code.