1 / 12 · Intro
What programming is
A program is a list of instructions, and JavaScript carries them out one statement at a time, from the top line to the bottom.
Jump to the 10 practice questionsYou will learn
- Follow a program one statement at a time, top to bottom
- Use comments and read the first error messages
1Explain
One statement, one line
Read it piece by piece. console is the place where a program writes messages for the person running it. .log means "write one line there." The parentheses hold what should be written, and the double quotes mark Hello, world! as text rather than as more code. The semicolon at the end closes the instruction, the way a full stop closes a sentence.
console.log("Hello, world!");Hello, world!2Explain
Top to bottom
A program with several statements runs them in order. The second line does not start until the first has finished. The program below prints three lines, and they come out in the same order as the code:
Press Next step: the orange line runs next, and each console.log adds one line to the output.
console.log("Wake up");console.log("Brush teeth");console.log("Eat breakfast");// prints:// Wake up// Brush teeth// Eat breakfast
Wake up Brush teeth Eat breakfast
3Problem
Two lines or one?
What does this print?
console.log("Hi");
console.log("there");Hint
Each console.log prints its own line, in the order of the code.
Solution
Answer: Hi, then there on the next line
Each console.log prints its own line, and statements run from top to bottom.
4Explore
What goes inside console.log( )
Pick what goes inside the brackets. Text in quotes is copied, a sum without quotes is worked out, commas put a space between values, and a word without quotes is read as a name.
console.log("Hello, world!");Hello, world!5Explain
// and /* */ comments
In the example above, the lines starting with // are comments. JavaScript skips everything from // to the end of that line, so you can write notes to yourself or to other readers. For a note that spans several lines, start it with /* and end it with */. Comments are also a handy way to switch a line off without deleting it: put // in front of it and it no longer runs.
console.log("Snack");
// console.log("Nap");
console.log("Walk");
/* console.log("Bath");
console.log("Bed"); */Snack
Walk6Problem
How many lines?
How many lines does this print?
console.log("A");
console.log("B");
/* console.log("C");
console.log("D"); */
console.log("E");Hint
Lines inside /* */ do not run, however many there are.
Solution
Answer: 3
C and D are inside a /* */ comment. A, B and E print.
7Practice
Hello, world!
Write one statement that prints Hello, world! exactly, with the capital H and the comma.
Hello, world!// Print the words Hello, world!
// on the line below.
Hint
Copy the text exactly inside double quotes: capital H, the comma and the exclamation mark.
A solution
console.log("Hello, world!");8Practice
Put the lines in order
Change the order of the statements so the output reads Line 1, Line 2, Line 3.
Line 1
Line 2
Line 3console.log("Line 3");
console.log("Line 1");
console.log("Line 2");
Hint
The output follows the order of the statements, so move whole lines.
A solution
console.log("Line 1");
console.log("Line 2");
console.log("Line 3");9Problem
A misspelled name
You run consol.log("Hi"). What happens?
Hint
Is there anything called consol?
Solution
Answer: It stops with a ReferenceError: consol is not defined
There is nothing called consol, so JavaScript reports that the name is not defined.
10Problem
A missing quote
A line is missing its closing quote: console.log("Hi);. What kind of error is this?
Hint
The code breaks the grammar of the language before anything runs.
Solution
Answer: SyntaxError
The code breaks the grammar of the language, so it is a SyntaxError and nothing runs.
11Explain
Reading your first errors
consol.log("Hi");ReferenceError: consol is not defined (line 1)
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- The line number shows where the program stopped. A SyntaxError stops it before any line runs.
A misspelled name: JavaScript knows console but not consol, so it stops with a ReferenceError on line 1.
console.log("Ready");
console.log("Go);SyntaxError: Invalid or unexpected token
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- The line number shows where the program stopped. A SyntaxError stops it before any line runs.
A missing closing quote breaks the grammar. It is a SyntaxError, so not even line 1 runs.
12
Summary
- A program is a list of instructions run from top to bottom.
- console.log("text") prints one line.
- Text goes inside quotes; the quotes are not printed.
- // starts a comment; JavaScript skips it.
- JavaScript is case sensitive.