By the end of this lesson you will be able to
- explain, in your own words, what a computer program is
- explain why instructions to a computer must be exact and in the right order
- say what JavaScript is, and why this course teaches it
- run a piece of code on this website for the first time
A computer only does what it is told
A computer cannot guess what you mean. It cannot use common sense, and it cannot fill in a gap the way a person can. It can only follow instructions, written in a language it understands, one step at a time. A program is simply a list of such instructions.
You give exact instructions all the time without thinking of it as programming. Suppose you asked someone who had never made tea before to make you a cup. You might say:
- Boil water in a pan.
- Add two spoons of tea leaves.
- Let it boil for two minutes.
- Add milk and sugar to taste.
- Pour through a strainer into a cup.
Every step matters, and so does the order. Add the milk before boiling and you get a different result. Skip the strainer and you get tea leaves in your cup. A computer follows instructions exactly like this: precisely, in order, with no assumptions of its own.
The instructions a programmer writes are called code, and writing them is called coding. Later in this course you will read words like “program”, “code” and “script” used to mean much the same thing: a set of instructions written for a computer to follow.
Why order and exactness matter
Here are two sets of instructions for crossing a road. Read both before deciding which is safer.
- Look both ways
- Wait for a gap
- Cross
Now the same three instructions, reordered:
- Cross
- Look both ways
- Wait for a gap
The instructions are identical. Only the order changed, and that alone makes the second version dangerous. A person can often notice a badly ordered instruction and correct it. A computer cannot: it will follow the steps precisely as written, in the order given, even into a mistake. This is the single most important idea in programming, and you will see it again in Lesson 1.2, where the order of your instructions decides the order the results appear on the screen.
Which of the two road-crossing lists above would a computer still follow exactly, even though it leads to a bad outcome?
A computer has no sense of “safer”. It runs the instructions it is given, in the order it is given them, whether or not that order makes sense.
What is JavaScript?
JavaScript is a programming language: a way of writing instructions that a computer can follow. There are many programming languages, in the same way there are many human languages, but JavaScript has a special place among them. It is the language that every web browser understands. Whenever a web page reacts to you (a menu that opens when you tap it, a form that warns you about a missing email address, a map you can drag around), JavaScript is almost certainly doing the work.
It is not only for web pages. The same language also runs on servers, in phone apps and in desktop programs, and it is used by businesses everywhere, from Bombay to Boston.
This course teaches JavaScript for three practical reasons:
- It is already on your device. Every modern browser, on every computer and phone, can run JavaScript. There is nothing to download, now or later.
- It is used everywhere. Almost every website in the world uses it, so the language you learn here is the same one professional web developers use every day.
- You can see your results. By the end of this course you will write JavaScript that changes a real web page: text that updates, buttons that respond, a form that checks what is typed into it.
Despite the similar name, JavaScript and Java are two different languages, about as closely related as a car and a carpet. The name was chosen in 1995 mainly for marketing reasons. Everything in this course is JavaScript.
How this course works
This is a text course with no videos, but it is not a course you only read. Every lesson has boxes of real, working JavaScript code, right here on the page. You read them, run them, change them, and see what happens.
Try it now: tap the blue Run button below the box. The result will appear underneath it.
Notice what just happened: nothing was installed, and nothing was sent to any server. Your browser already knows JavaScript, so it simply ran the code and showed you the result. That is how every code box on this course works, from here to the very last lesson.
You do not yet know what that line of code means; that is exactly what Lesson 1.2 teaches, in detail. For now, try changing the words inside the quotation marks to a sentence of your own, and press Run again.
Each time you press Run, this page sets up a small, sealed-off practice area and runs your code inside it. Your code cannot change this lesson page, cannot see your login, and cannot reach the internet, so you can experiment freely: nothing you type can break anything. If a program accidentally runs for ever, the page stops it after a few seconds and tells you so.
What you will build
Programming is learned by doing, not by reading about it, so this course is built around short, real tasks rather than long explanations. Across the coming lessons in this module you will:
- write your first working program, using an instruction called
console.log - use JavaScript as a calculator
- store information the program can use later, in what programmers call variables
Later modules build on this, through decisions, repetition and small complete projects, and finish with working web pages that respond to clicks and typing. Each lesson assumes only what earlier lessons taught, so if a word is unfamiliar, it will have been explained already or will be explained where it first matters.
Quick check
Three questions to confirm the main ideas before you move on.
1. What is a computer program?
A program is the set of instructions itself, not the machine, and not a guess. The computer follows it exactly.
2. Why does the order of instructions matter to a computer?
A computer has no common sense to fall back on. It runs your instructions exactly as ordered, so a wrong order gives a wrong (or dangerous) result without any warning.
3. What do you need to install on your own computer to do the exercises in this course?
JavaScript is built into every modern web browser, so the code boxes work straight away. (And Java is a different language altogether.)
Summary
- A program is a list of exact instructions for a computer to follow, one step at a time.
- A computer cannot guess your meaning or notice a mistake in the order of instructions; it follows them exactly as given.
- JavaScript is the programming language that every web browser understands. It makes web pages interactive, and it is used far beyond the browser too.
- Every code box on this course runs real JavaScript in a sealed-off practice area of the page. Nothing needs to be installed, and nothing you type can break anything.
Found a mistake on this page, or something unclear? Report a problem and mention “JavaScript Lesson 1.1”.
