What a Program Does

Welcome to your first JavaScript course! By the end of it, you’ll have a tiny movie night snack calculator running on your own computer. It prints a message of your choice and works out what two bags of popcorn cost. That’s small on purpose, because bigger programs grow from the same process of writing, running, checking, and changing.

You don’t need any programming experience, only the ability to install an app and find a file. You’ll set up your tools over the next few lessons. In this one, there’s no JavaScript yet. You’ll explain what a program is, then write exact instructions and predict what goes wrong when a step is vague, missing, or out of order.

Understand What a Program Is

A program is a list of exact instructions for a computer. In the short programs in this course, the computer follows them in order. It doesn’t guess what you meant or fill in a missing step. That strictness makes it reliable, so your job is to write instructions that give the right result when followed literally.

Give Instructions to a Literal-Minded Helper

Imagine a kitchen helper who, like a computer, follows instructions exactly as written. If a step is unclear, it stops and waits. If the order is strange, it follows it anyway.

You’d like a cup of tea with milk, which needs 250 milliliters of boiling water on a tea bag, three minutes before the bag comes out, and one tablespoon of milk. The kettle starts empty, and tap water is cold. You hand your helper these steps:

  1. Fill the kettle with 500 milliliters of water.
  2. Put a tea bag in the mug.
  3. Pour 250 milliliters of water from the kettle into the mug.
  4. Switch on the kettle and wait until the water boils.
  5. Add some milk.
  6. Take out the tea bag.

Before reading on, trace these steps as your helper would, checking each against that goal. Where does it go wrong, and what ends up in the mug?

Steps 3 and 4 are swapped. Your helper pours 250 milliliters of cold water onto the tea bag, then boils the other 250 milliliters for nobody. It follows each step as written, but the order is wrong.

Step 5 is vague. “Some” doesn’t say one tablespoon, so your helper stops and waits.

There’s also a missing step. No step says to wait three minutes, so step 6 would take the bag out straight away and leave weak tea. Here’s a fixed version:

  1. Fill the kettle with 500 milliliters of water.
  2. Switch on the kettle and wait until the water boils.
  3. Put a tea bag in the mug.
  4. Pour 250 milliliters of water from the kettle into the mug.
  5. Wait three minutes, then take out the tea bag.
  6. Add one tablespoon of milk.

This comparison has a limit. With the first list, your helper stopped after step 4. A computer can’t ask, and JavaScript code it can’t understand can stop a program before even its first line runs. The computer shows a message instead, which you’ll learn to read as a hint later in this course.

Now it’s your turn. The widget below starts with the original six steps. Move a step with its arrows, choose how much milk to add, and tick the box to add the missing wait, then click Run and the helper follows it. Fix the swapped pair, the vague step, and the missing wait so the helper makes a proper cup of tea:

Interactive diagram — enable JavaScript to view.

Each fix changed what the helper did, because it only ever does what the list says. Without the widget, trace your repaired list on paper against the fixed version above.

Now write a list of your own: four to six steps for one slice of buttered toast. The bread is in its bag, the toaster is plugged in, the butter is in the fridge, and a knife and a plate are on the counter. The goal is one toasted slice on the plate, buttered on one side.

Then trace it as the helper would. For each line, check that earlier lines made it possible and that it says exactly which thing, how much, or how long. Compare the final plate with the goal, fix any line that fails, and trace again. Programmers repeat this loop all the time.

For a second opinion, the button below opens a chat with your mentor. Paste your toast steps after the question, and the mentor will follow them and say where it gets stuck.

Tell Source Code Apart From Running It

Writing a list of tea steps doesn’t heat any water. Tea only happens when the helper follows it.

Programs have the same two sides. The written instructions are called source code, or just code, which is text you type and save in a file. Running a program means telling the computer to follow that code. The simple programs in this course run from the first line to the last and then finish. So if you edit a price in your snack calculator, nothing happens until you save the file and run it again.

Know Where JavaScript Runs

JavaScript is a language for writing source code. Running it needs a program that understands JavaScript and plays your kitchen helper’s part. Every modern web browser has one built in, which is why many website buttons and menus work. MDN’s What is JavaScript? shows more.

JavaScript can also run outside a browser, using a program called Node.js, or Node for short. With Node, you save JavaScript files on your computer and run them in a text window, the terminal. That’s how you’ll work in this course, with programs that are files you own.

Before moving on, answer these in your own words, out loud or in a note. What is a program? What’s the difference between source code and running it? Where can JavaScript run? Then compare your answers with these points.

  • Program: Exact instructions that a computer follows in order, without guessing.
  • Source code and running: Source code is the saved text of the instructions. Running is the computer following them, so an edit does nothing until you run again.
  • Where JavaScript runs: In web browsers and outside them with Node.

If an answer missed a point or mixed up writing with running, reread that section and revise your answer. For feedback on your wording, use the button below and add your explanation.

In the next lesson, you’ll set up your editor and a course folder where your programs will live.

Become a Member to join the conversation.