Loading...

JavaScript essentialsGetting Started

Syntax, statements & comments

7 min lesson

Next
  • Syntax means the rules for how to write JavaScript so the computer can understand it, just like grammar rules for a spoken language.
  • You need it because even a tiny typo, like a missing quote, stops the whole script from running.
  • Think of syntax as the shape of a key: if the shape is even slightly wrong, the key will not fit the lock.
  • A statement is one complete instruction, like console.log("hi"); and it usually ends with a semicolon.
  • A comment is a note for humans that the computer ignores, written with // for one line or /* */ for several lines.
  • The basic form is: a statement on its own line, ending in a semicolon, with optional comments above it.
  • Real code uses comments to explain why something is done, not just what it does.
  • An easy-to-miss detail: JavaScript is case-sensitive, so Console.log is not the same as console.log and will fail.
  • This lesson builds on the previous scripts by showing you how to format them properly and add helpful notes.
  • After this lesson you should be able to write clean, commented statements and spot simple syntax errors.
Keep in mind
  • Syntax is the set of rules for writing valid JavaScript.
  • End statements with a semicolon and match your quotes and brackets.
  • JavaScript is case-sensitive, so copy names exactly.
  • Comments are your friends; they explain your code to your future self.
syntax.js
// This program shows correct syntax and helpful comments.
// Each statement ends with a semicolon.

// Declare a variable called score and give it the value 10.
let score = 10;

// Print a label and the score together.
console.log("Score:", score);

// Increase the score by 5 using the += operator.
score += 5;

// Print the new score to confirm the change.
console.log("New score:", score);

What happens: Score: 10 New score: 15

Breaking it down

Single-line comment

// Declare a variable

Everything after // on that line is ignored by the computer. It is there to explain the code to humans.

Variable declaration

let score = 10;

This creates a named box called score and puts the number 10 inside. The semicolon ends the statement.

Function call

console.log("Score:", score);

This prints the text and the value of score. Commas separate the pieces to print. The semicolon ends the statement.

Compound assignment

score += 5;

This adds 5 to the current value of score and stores the result back in score. It is a shortcut for score = score + 5;

Syntax symbols and their jobs

SymbolJobExample
//Starts a one-line comment// note
;Ends a statementlet x = 1;
" "Wraps text as a string"hello"
=Assigns a valuelet x = 5;

A helpful analogy

Syntax is like the shape of a key. A key only opens the door if its grooves match exactly. JavaScript only runs your code if the quotes, brackets and semicolons match its rules. A single wrong groove and the key will not turn.

Nesting rules

  • 1.Match every opening quote with a closing quote of the same type.
  • 2.End each statement with a semicolon so the computer knows where it stops.
  • 3.Use // for short notes and /* */ only when you need several lines at once.

Common mistakes

Wrong capitalisation

Avoid

Console.log("hi"); // error: Console is not defined

Use instead

console.log("hi");

JavaScript is case-sensitive. Console with a capital C is a different name that does not exist.

Missing closing quote

Avoid

console.log("hi); // syntax error

Use instead

console.log("hi");

The computer keeps reading looking for the closing quote and never finds it, so the whole line fails.

Quick tips

  • •Read error messages in the console slowly; they often point to the exact line.
  • •Put a space after // before your comment text so it is easy to read.
  • •Try removing a semicolon and see what error appears, then put it back.
Previous
Next