Lesson 28 of 296
Statements, comments and console.log
What you will learn
- What a statement is
- Semicolons
- How to write comments
- How
console.logworks - Other useful console methods
On this page
Statements
A statement is one complete instruction. A program is a list of statements. The engine runs them from top to bottom, one after another.
console.log("First");
console.log("Second");
console.log("Third");Shows:
First
Second
ThirdSemicolons
Statements usually end with a semicolon ;. JavaScript can often add missing semicolons for you (this is called automatic semicolon insertion), but it sometimes guesses wrong. Most teams either always write semicolons, or use a tool (such as Prettier) that handles them. In this course we always write them.
Comments
A comment is a note for humans. The engine ignores it completely.
// This is a one-line comment
/* This is a comment
over several lines */
const vatRate = 0.2; // UK standard VAT rateA good comment explains why the code does something, not what it does. The code already shows what.
Useless: const total = price * 100; // multiply price by 100
Useful: const total = price * 100; // store money in pence to avoid decimal errorsconsole.log
console.log() shows a value so you, the developer, can see it.
- In the browser, it shows in the Console tab of dev tools. Normal visitors do not see it.
- In Node.js, it shows in the terminal.
console.log("Hello"); shows: Hello
console.log(5 + 3); shows: 8
console.log("5 + 3"); shows: 5 + 3How it works:
- Whatever is inside the brackets is worked out first.
- Then the result is shown.
So 5 + 3 (no quotes) is maths and shows 8. "5 + 3" (in quotes) is text and is shown exactly as written.
Several values at once, separated by commas:
const name = "Sam";
const age = 30;
console.log("Name:", name, "Age:", age);Shows:
Name: Sam Age: 30console.log only shows a value. It does not change anything in your program.
Other console methods
| Method | Use |
|---|---|
console.log() | Normal messages |
console.error() | Error messages, shown in red |
console.warn() | Warnings, shown in yellow |
console.table() | Shows an array or object as a table |
console.clear() | Clears the console |
console.table([
{ name: "Sam", age: 30 },
{ name: "Ali", age: 25 }
]);This prints a neat table with columns "name" and "age".
Spaces and new lines
JavaScript mostly ignores extra spaces and empty lines. These two lines do the same thing:
console.log(5+3);
console.log( 5 + 3 );Use spaces to make code easier to read. Tools such as Prettier format code automatically so the whole team writes it the same way.
Case sensitivity
JavaScript is case-sensitive. Capital and small letters are different.
console.log("hi"); works
Console.log("hi"); Error: Console is not definedCommon mistakes
- Writing
Console.logorconsole.Logwith capitals. - Spelling it
consol.log. - Thinking quotes do not matter:
console.log(total)andconsole.log("total")are completely different. - Leaving dozens of
console.loglines in finished code. Remove them before you share your code.
How to say it in an interview
"A JavaScript program is a sequence of statements run from top to bottom. I end statements with semicolons for clarity. console.log prints values for debugging, and I also use console.error, console.warn and console.table. I use comments to explain why code exists, not what it does."
Key words
| Word | Meaning |
|---|---|
| Statement | One complete instruction |
| Semicolon | The ; that ends a statement |
| Comment | A note the engine ignores |
| console.log | Shows a value in the console |
| Case-sensitive | Capital and small letters are treated as different |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.