Learn / Lesson 1 of 10
What code is, and how to run it
Write your first line of JavaScript and see what the computer does with it.
Words on this page, in plain English (2)Open if any word is new to you
- Array (list)
- A list of values kept in order, inside square brackets. Like a row of numbered lockers.
- console.log
- Prints something so you can see it. The easiest way to find out what your code is really doing.
A program is a list of instructions
A computer can't guess what you mean. It follows instructions exactly, one after another, from top to bottom. A program is just that: a list of instructions, written in a language the computer understands.
We'll use JavaScript. It runs in every web browser, so you don't need to install anything: the boxes on this site run your code right here on the page.
Your first line of code
console.log(...) tells the computer to show something. Whatever you put between the brackets appears in the output. Press Run:
Try it: say hello
console.log("Hello!");Press Run to see the output. You can change the code first.
Now change the text between the quotes to your own name, and press Run again. That's programming: change the instructions, run them, see what happens.
Many instructions, in order
Each line is one instruction. They run in order, top to bottom:
Try it: three lines
console.log("First");
console.log("Second");
console.log("Third");Press Run to see the output. You can change the code first.
Try swapping two lines, and run it again. The output follows the order of the code.
The computer can do maths
Without quotes, numbers are numbers, and the computer works out the answer:
Try it: a calculator
console.log(2 + 3);
console.log(10 - 4);
console.log(6 * 7); // * means multiply
console.log(20 / 4); // / means divide
console.log("2 + 3"); // in quotes, it's just textPress Run to see the output. You can change the code first.
Notice the last line: in quotes, 2 + 3 is just text, shown as it is. Without quotes, it's a sum to work out. We'll come back to that difference in the next lesson.
Comments: notes for humans
Anything after // on a line is a comment. The computer ignores it. Comments are notes for people reading the code, like the ones above explaining * and /.
// This line is ignored by the computer.console.log("This one runs"); // and so is everything after the slashes hereErrors are normal
Every programmer sees errors all day. An error is the computer saying "I don't understand this instruction". It's not a disaster; it's a clue. Run this, and read what it says:
Try it: a broken line
console.log("Hello";Press Run to see the output. You can change the code first.
The error points at the line, says what's wrong in JavaScript's words, and explains it in plain English. Here a ) is missing. Fix it and run again.
When you see an error: read the line number, read the message, and look carefully at that line and the one just before it.
Your turn
Write three lines that show your name, your favourite number, and that number multiplied by 2.
Your turn
// Write your three console.log lines below.Press Run to see the output. You can change the code first.
What you learned
- A program is a list of instructions, run from top to bottom.
console.log(...)shows a value.- Text goes in quotes; numbers and sums don't.
//starts a comment.- Errors tell you where to look. Read them!