Skip to the content

Lesson 28 of 296

Statements, comments and console.log

What you will learn

  • What a statement is
  • Semicolons
  • How to write comments
  • How console.log works
  • 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
Third

Semicolons

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 rate

A 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 errors

console.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 + 3

How it works:

  1. Whatever is inside the brackets is worked out first.
  2. 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: 30

console.log only shows a value. It does not change anything in your program.

Other console methods

MethodUse
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 defined

Common mistakes

  • Writing Console.log or console.Log with capitals.
  • Spelling it consol.log.
  • Thinking quotes do not matter: console.log(total) and console.log("total") are completely different.
  • Leaving dozens of console.log lines 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

WordMeaning
StatementOne complete instruction
SemicolonThe ; that ends a statement
CommentA note the engine ignores
console.logShows a value in the console
Case-sensitiveCapital and small letters are treated as different

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.