Live Demos

We start together with three short live demos. After that you work through the page on your own: three checkpoints, then the lab.

  1. Setup check, everyone at once. Open an HTML file in a real browser tab and open the console, so alert(), prompt() and console.log() all work before you start.
  2. console is also where you get error messages
  3. Idea of a variable
  4. Go over the difference between numbers and strings
  5. Go over prompt()
  6. prompt() always hands back text. Type 12 into a prompt(), add 1 to it, and see what comes out. (Slide: Getting User Input: prompt())

Demo file: Samples/js_02_live_demo.html

<!DOCTYPE html>
<html>
<body>

<script>
  alert("Hello from alert()");

  let name = prompt("What is your name?");

  console.log("Hello from console.log()");
  console.log(name);
</script>
</body>
</html>
Notes

Adding Javascript to a page: External Files

In the previous lesson, JavaScript was written directly inside the HTML file. You can also write JavaScript in a separate .js file and link to it:
<!DOCTYPE html>
<html>
<head></head>
<body>

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

</body>
</html>
The file myscript.js contains only JavaScript, no <script> tags needed:
// content of myscript.js
console.log("hello");
alert("world");

There are three ways to include JavaScript altogether:
<body>

  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>  <!-- 1. from another server -->
  <script src="./js/myscript.js"></script>                             <!-- 2. local external file -->
  <script> alert('hello'); </script>                                    <!-- 3. embedded directly -->

</body>
In this course we will write our JavaScript embedded directly inside <script> tags (method 3). You will still see external files in other people's code and whenever you load a library.
Ask:
"Why would I put my JavaScript in a separate .js file instead of writing it directly in the HTML? What are the advantages?" "What is jQuery? Why is it loaded from an external server URL instead of a local file?"
Notes

Checkpoint 1: Your First Script

Try it:
  1. In VS Code, create a new file called checkpoints.html. You will keep adding to this same file in Checkpoints 2 and 3.
  2. Paste this in and replace YOUR NAME with your name:
<!DOCTYPE html>
<html>
<body>


   
<script>
  console.log("YOUR NAME");
</script>
</body>
</html>
  1. Save, then open the file in a real browser (double-click it, or "Open with Live Server"). See "Previewing Your HTML/JS File" at the top of this page.
  2. Open the console. In Chrome: View → Developer → JavaScript Console (⌘⌥J).
You will kwnow it worked when your name appears in the console.
Nothing showing? Reload the page with the console open, and check the spelling of console.log. Capital letters matter.
Notes

Statements

console.log('hello world');
alert('yikes');

Write a statement, add a semicolon, start a new line for the next statement.
Ask:
"What happens if I forget the semicolon at the end of a JavaScript statement? Will it always cause an error?" "Why is JavaScript case-sensitive? What error would I get if I typed Alert('hello') instead of alert('hello')?"
Notes

Comments

let brushSize = 10;  // this is a single-line comment

/*
  This is a multi-line comment.
  Author: Jane Smith
  This script draws a grid of colored squares.
*/
Comments are ignored by the browser. They are notes for the programmer. Use them to explain why the code does what it does, not just what it does.
Ask:
"When should I write a comment in my code? Give me an example of a useful comment vs. an obvious/unnecessary comment." "What is the difference between // and /* */ comments in JavaScript?"
Notes

Variables

You may already be familiar with variables from math: x and y stand in for a value that can change. In programming it is the same idea: a variable is a named container for a value.


You can read the value, change it, and reuse it, for example to store a brush color, the size of a canvas, or a viewer's name.

Note: older JavaScript code uses var instead of let. They work similarly here, but let is the modern standard, so use it.

Ask:
"What is a variable in programming? Explain it as if I have never coded before." "What is the difference between let, const, and var in JavaScript? Which one should I use and when?"
Notes

Creating and Using a Variable

let declares (creates) the variable and gives it a name. You can then assign a value to it with =.

Copy, paste, and run this file. Predict each console output before you open the console:
<!DOCTYPE html>
<html>
<body>
<script>
  let canvasWidth;          // declare: the container exists, but it is empty (undefined)
  console.log(canvasWidth); // what prints here?

  canvasWidth = 800;
  console.log(canvasWidth); // what prints here?

  canvasWidth = 1200;       // overwrite with a new value
  console.log(canvasWidth); // what prints here?

  // declare AND assign in one line
  let margin = canvasWidth * 0.1;
  console.log(margin);      // what prints here?

  // variables can hold text too, called a string
  let artworkTitle = "Blue Study";
  console.log(artworkTitle);
</script>
</body>
</html>
When storing text (strings), always wrap it in quotes. Without quotes, JavaScript thinks it is a variable name, not text.
  let artworkTitle = "Blue Study";  // correct: text in quotes
  let artworkTitle = Blue Study;  // error! JS looks for two variables named Blue and Study
Ask:
"What does 'undefined' mean in JavaScript? What does it mean when a variable is declared but not assigned a value?" "What is a string in JavaScript? Why do I need quotes around text but not around numbers?" "What happens if I use a variable before I declare it with let? Try to predict the error, then ask Copilot to confirm."
Notes

Variables and Arithmetic

JavaScript uses standard arithmetic operators: + - * /

Copy, paste, and run. Predict each result before opening the console:
<!DOCTYPE html>
<html>
<body>
<script>
  let panelCount    = 2;      // a diptych: one painting split into two panels
  let paintingWidth = 24.5;   // inches
  let frameWidth    = 3.75;
  let totalWidth    = paintingWidth + frameWidth;

  console.log(totalWidth);   // what is this?

  let panelWidth = paintingWidth / panelCount;
  console.log(panelWidth);

  // operator precedence: * and / happen before + and -
  // use parentheses to control the order

  let a = 1;
  let b = 2;
  let c = 3;

  console.log( a + b + c / 3 );    // NOT the average. Why?
  console.log( (a + b + c) / 3 );  // the average. Why is this different?
</script>
</body>
</html>

There are also shorthand operators for incrementing and updating variables:
  panelCount++;            // same as panelCount = panelCount + 1
  panelCount--;            // same as panelCount = panelCount - 1

  totalWidth += 5;         // same as totalWidth = totalWidth + 5
  totalWidth -= 2;         // same as totalWidth = totalWidth - 2
Ask:
"In JavaScript, what is operator precedence? Why does a + b + c / 3 not calculate the average?" "What does panelCount++ mean? Is it any different from panelCount = panelCount + 1?"
Notes

Using + with Strings (Concatenation)

+ does two different things depending on what it is given: Copy, paste, and run. Predict each alert before clicking OK:
<!DOCTYPE html>
<html>
<body>
<script>
  let firstname = "Amanda";
  let lastname  = "Reed";

  alert(firstname + lastname);         // what does this show?
  alert(firstname + " " + lastname);   // and this?

  // mixing a string and a number
  let series = "Untitled #";
  let number = 3;
  alert(series + number);              // what does this show?

  // tricky: what about number + number when one is actually a string?
  let a = "5";
  let b = 3;
  alert(a + b);   // addition or concatenation? predict first
</script>
</body>
</html>

operator what it does expression result
+ addition 5 + 1 6
+ concatenation "Reed" + " " + "College" "Reed College"
- subtraction 4 - 2 2
* multiplication 2 * 3 6
/ division 6 / 3 2
% modulus (remainder) 10 % 3 1
Ask:
"In JavaScript, if I write '5' + 3 (where '5' is a string), do I get 8 or '53'? Why does JavaScript do this?" "What is type coercion in JavaScript? Why does it happen with the + operator but not with - or *?"
Notes

Checkpoint 2: A Wall Label

Try it: in checkpoints.html, below your console.log line and inside the same <script>, create four variables:
  • artistName: your name (a string)
  • title: "Untitled #" (a string)
  • number: any number, for example 3
  • year: 2026 (a number)
Then use + to build one alert() that reads like a gallery wall label:
Untitled #3, Amanda Reed, 2026
You will keep working on checkpoints.html in Checkpoint 3 and submit it at the end (see "What to Submit").
Fix the bugs: the code below has three bugs. Paste it into a new file (nobug.html), open it in the browser, and read the console error. Copy the error message to AI and ask what it means, then fix each one. You can use agent mode.
<!DOCTYPE html>
<html>
<body>
<script>
  let firstName = "Alex"
  let lastName = "Park";
  let fullName = firstName + " " lastName;

  let printPrice = 40;
  let Prints = 3;
  let total = printPrice * prints;

  console.log(fullName + " sold " + prints + " prints for $" + total);
</script>
</body>
</html>
Hint: there is a missing semicolon, a missing operator, and a case mismatch. One of these does not cause an error at all. Which one, and why? (Look back at the Ask box on Statements.) The bug is gone when the console shows Alex Park sold 3 prints for $120 with no red errors.

Using comments (this is a comment section /* */, you can also use // for one line comments), right down what you did to fix it. Put nobug.html in your JS02 folder (see "What to Submit").
Notes

Getting User Input: prompt()

prompt() is a built-in browser method that shows a dialog box with a question. Whatever the user types and clicks OK, prompt() returns that text back to your code, always as a string. We use a variable to catch and store it. This is exactly what variables are for.


The key connection to variables:
let name = prompt("What is your name?");
//  ^^^^                                  the variable catches what prompt() returns
//         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ prompt() shows this question and returns the answer

Copy, paste, and run this complete example:
<!DOCTYPE html>
<html>
<body>
<script>
  let name  = prompt("What is your name?");
  alert(name + ", welcome to the studio!");

  let color = prompt("What color are you drawn to lately, " + name + "?");
  alert(color + "? Good choice for a first sketch.");

  let hours = prompt("How many hours a week do you spend making art?");

  // Note: prompt() always returns a string.
  // Number() converts it to a number so we can compare it.
  // (We will cover if/else properly in a later lesson.)
  if (Number(hours) < 5) {
    alert(hours + " hours? Every sketchbook starts somewhere.");
  } else {
    alert(hours + " hours? You have a real practice going!");
  }
</script>
</body>
</html>
Note: We haven't look at if / else yet. For now, just observe what it does.
Ask:
"What does it mean when a function 'returns' a value in JavaScript? What does prompt() return?" "Why do I need a variable to use the result of prompt()? What would happen if I wrote just prompt('What is your name?') with no variable?" "If the user types the number 12 into prompt(), what type of value do I get back, a number or a string? What does Number() do about that?" "What happens if the user clicks Cancel on a prompt() dialog? What value does the variable hold?"
Notes

Checkpoint 3: Ask the Viewer

Try it: back in checkpoints.html, change your wall label so the viewer supplies the title.
  1. Replace the fixed title with a question:
      let title = prompt("What is the title of your piece?");
    
  2. Keep artistName and year, and update your alert() so the label uses the typed title.
  3. Add one more prompt(), for example the medium ("oil on canvas", "digital"), and add it to the label.
You know it worked when: the alert shows exactly what you typed, for example Night Walk, Amanda Reed, 2026, oil on canvas.
Then run it again and click Cancel instead of typing. What shows up in the label? Ask AI why. Submit checkpoints.html (see "What to Submit").
Notes

Lab: Build a Conversation

Recall the conversation example using prompt() and alert(). Use that as a starting point, or your checkpoints.html which already asks the viewer a question and answers with a label. Create conversation.html that talks to its viewer: a gallery guide, a fortune teller, a character with a mood, an interview.

Using prompt() and alert(), create a short conversation with the viewer. Your script must:
  • Ask at least three questions using prompt()
  • Store each answer in a well-named variable
  • Use the answers in at least one alert() that combines strings and variables with + (for example: "Amanda, you called your piece Night Walk. What time of night is it?", where Amanda and Night Walk come from prompt())
  • Ask your agent to add if / else statements so the conversation changes depending on the viewer's answer (for example, if the viewer says A, respond one way, otherwise respond another way)

Start from your checkpoints.html, or from this blank file:
<!DOCTYPE html>
<html>
<body>
<script>
  // write your prompt() and alert() calls here

</script>
</body>
</html>
Notes

What to Submit

Create a folder on your Reed server:
~myname/ILCP/JS02/
Put these three files in it:
  1. checkpoints.html: Checkpoints 1, 2 and 3 in one file (the wall label that uses prompt() for the title and medium)
  2. nobug.html: the fixed bug code, with a comment explaining what you fixed
  3. conversation.html: the Lab, a short conversation with the viewer using prompt(), alert() and if / else
Check before you leave: open each file from your Reed server URL in a browser (for example https://people.reed.edu/~myname/ILCP/JS02/checkpoints.html) and make sure it runs with no red errors in the console.
Notes