Chapter 01
What Is Programming?
A computer is fast and precise, but it has no common sense. It does exactly what you tell it, in the exact order you tell it, and nothing more. Programming is the act of writing those instructions down in a language the computer understands.
Think of a recipe. "Crack two eggs, add a pinch of salt, whisk for thirty seconds." Each step is clear and happens in order. A program is the same idea: a list of small, unambiguous steps. The language we'll use to write them is JavaScript.
Opening the Console
The Console is a place where you can type one line of JavaScript, press Enter, and instantly see what it does. To open it:
- Press
F12on your keyboard. (On a Mac you can also useCmd + Option + J.) - A panel opens - this is the developer tools. Click the tab labelled
Console. - You'll see a blinking cursor. That's where you type.
Leave this page open, open the Console next to it, and type along with every example. Reading code teaches you very little; running it and changing it teaches you everything.
Your First Line of Code
Type this into the Console exactly as shown, then press Enter:
console.log("Hello, world!");The Console prints:
Hello, world!Congratulations - you just ran a program. Let's break down what you wrote:
console.logis a built-in command that means "print this out so I can see it." It's the tool you'll use most while learning.- The round brackets
( )hold the thing you want to print. - The text
"Hello, world!"is wrapped in quotes because it's a piece of text, not a command. More on that in Chapter 3. - The semicolon
;marks the end of the instruction, like a full stop at the end of a sentence.
Try Changing It
Change the text between the quotes to your own name and run it again:
console.log("My name is Sam");
console.log("I am learning to code");Notice the two lines run one after the other, top to bottom. That order matters - it's the single most important idea in all of programming. The computer never skips ahead or guesses; it walks down your instructions one at a time.
Notes to Yourself: Comments
Sometimes you want to leave a note in your code for a human to read - not an instruction for the computer. Anything after two slashes
//is ignored by JavaScript:Comments are how you explain your thinking. Use them freely while you're learning.