JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 1: Discovering JavaScriptLesson 3 of 150About 40 minutes

Lesson 3: Statements, expressions and comments

01Opening question

In let total = price * 2;, which part does something and which part produces a value?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

Run it. The expression price * 2 is worked out to 24, then the statement stores it in total.

const price = 12;
let total = price * 2;   // price * 2 is an expression
console.log(total);

03Objective and prerequisites

Learning objective: Tell statements from expressions, predict how operator precedence shapes a result, and use comments to explain code without changing what it does.

You need: Lesson 2: Your first JavaScript program

Skills practised: JavaScript basics, Expressions and operators.

04Explanation

An expression is any piece of code that produces a value: 5, "hi", price 2 and (4 + 6) 2 are all expressions.

A statement is a complete instruction that does something: declare a variable, change it, print something. Statements are usually one line and often end with a semicolon.

When an expression has several operators, JavaScript follows precedence rules, like maths: * and / happen before + and -. Parentheses ( ) let you choose the order.

A comment is a note for humans. JavaScript skips it completely. // starts a comment that runs to the end of the line, and / ... / can wrap several lines. Comments are also handy for switching off a line while you investigate.

05Definitions

Expression
Code that produces a value, such as 3 + 4 or price * quantity.
Statement
A complete instruction, such as a declaration or an assignment.
Operator
A symbol that works on values, such as +, * or %.
Operator precedence
The rules that decide which operator in an expression is applied first.
Comment
A note in the code that JavaScript ignores: // ... or / ... /.
Remainder operator (%)
Gives what is left over after division: 10 % 4 is 2.

Open the full glossary

06Visual learning object

Mathematical expression evaluator. Type an expression and see the order in which JavaScript evaluates each part.

Loading the interactive object…

Open the full Mathematical expression evaluator page with its guided experiment.

07Examples

ex-003-01Expressions produce values Browser-executable JavaScript

Each console.log receives the value of an expression. Precedence and parentheses change the grouping.

console.log(4 + 6 * 2);
console.log((4 + 6) * 2);
console.log(10 / 4);
console.log(10 % 4);
Sample input
None
Expected output
16 20 2.5 2
Common mistake
Reading left to right like a calculator: 4 + 6 * 2 is 16, not 20.
Try this
Write an expression that uses all of + * and ( ) and equals 50.
Source
examples/ex-003-01-expressions-produce-values.js

ex-003-02Statements do work Browser-executable JavaScript

Declarations and assignments are statements. The final value of total depends on each statement running in order.

let total = 0;          // declaration statement
total = total + 12;     // assignment statement
total = total + 8;
console.log(total);     // expression statement that calls console.log
Sample input
None
Expected output
20
Common mistake
Thinking total + 8; on its own changes total. Without total = the result is thrown away.
Try this
Add a line that doubles total before it is printed.
Source
examples/ex-003-02-statements-do-work.js

ex-003-03Comments are ignored Browser-executable JavaScript

Comments never run. A commented-out console.log prints nothing.

// This whole line is a comment: JavaScript skips it.
console.log("Visible line");
// console.log("This line is switched off");
/*
  A block comment can span
  several lines.
*/
console.log("Another visible line"); // a comment can follow code
Sample input
None
Expected output
Visible line Another visible line
Common mistake
Starting a block comment with / and forgetting the closing /, which comments out the rest of the file.
Try this
Switch the third line back on by removing the //.
Source
examples/ex-003-03-comments-are-ignored.js

ex-003-04A value nobody looks at Browser-executable JavaScript

An expression statement is evaluated, but if its value is not printed or stored, nothing visible happens.

const word = "JavaScript";
word.length;              // calculated, then thrown away
console.log(word.length); // calculated and printed
"Unused text";
console.log("Done");
Sample input
None
Expected output
10 Done
Common mistake
Expecting a value to appear just because a line computes it. Only console.log prints.
Try this
Store word.length in a constant named letters and print letters * 2.
Source
examples/ex-003-04-unused-values.js

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Fix the average with parentheses

Use parentheses to change the order of evaluation.

const average = 80 + 90 / 2;
console.log("Average:", average);

Switch off the debug lines

Use comments to disable code without deleting it.

console.log("Order received");
console.log("DEBUG: raw quantity = 3");
console.log("Order total: 75");
console.log("DEBUG: discount skipped");

Weekly pay calculator

Combine several expressions to solve a practical calculation.

// Hours worked: 38 at a rate of 22 per hour.
// Overtime: 6 extra hours paid at 1.5 times the rate.

09Prediction

Commit to an answer first, then run the real code to compare.

let x = 3;
x = x * 2 + 1;
// x = 100;
console.log(x);
console.log(x + 1 * 2);

What two lines does this print?

10Debugging investigation

The average score is 180

A grade report should show the average of three quiz scores (70, 80 and 90). It prints 180, which is higher than any single score.

const quiz1 = 70;
const quiz2 = 80;
const quiz3 = 90;
const average = quiz1 + quiz2 + quiz3 / 3;
console.log("Average score:", average);

More cases in the Debugging Detective Laboratory

11Practical scenario

Shopping cart arithmetic

A cart total is a chain of expressions: line totals, a subtotal, a discount and tax. Get the grouping wrong and customers are charged the wrong amount. The Shopping Cart project's Stage 1 asks you to write exactly these expressions and check them.

const subtotal = 25 * 2 + 40;
const afterDiscount = subtotal - subtotal * 0.1;
console.log(subtotal, afterDiscount);

12Knowledge check

1. What does this print?

console.log(2 + 3 * 4);
  • 20
  • 14
  • 24
  • 9

2. Which of these is an expression?

  • let count = 0;
  • price * 3
  • // total
  • const x;

3. What does this print?

console.log(17 % 5);
  • 3.4
  • 2
  • 3
  • 5

4. This should print 15 but prints 10. Why?

let total = 10;
total + 5;
console.log(total);
  • + does not work with let
  • total + 5; calculates 15 but never stores it
  • console.log prints the first value only
  • The semicolons are wrong

5. Both lines compute an average. Which is correct for scores a = 60 and b = 80?

// A
const avgA = a + b / 2;
// B
const avgB = (a + b) / 2;
  • A only
  • B only
  • Both
  • Neither

6. What happens to a line that starts with //?

  • It runs twice
  • It is printed to the console
  • JavaScript ignores it
  • It causes an error

13Solutions and tests

Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.

Automated acceptance tests for this lesson

These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.

  • Fix the average with parentheses: Prints Average: 85; average holds 85; Uses parentheses
  • Switch off the debug lines: Only the customer lines print; The DEBUG lines are kept as comments
  • Weekly pay calculator: Prints the three pay lines; Constants hold the given values; Includes an explanatory comment
  • ex-003-01: output matches the documented expected output
  • ex-003-02: output matches the documented expected output
  • ex-003-03: output matches the documented expected output
  • ex-003-04: output matches the documented expected output

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

The lesson lives in the companion repository at curriculum/level-01-foundations/module-01-discovering-javascript/lesson-003-statements-expressions-and-comments/. Every example on this page is generated from those files.

These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.

15Summary and next steps

  • Expressions produce values; statements do things.
  • Precedence decides the order of operators; parentheses override it.
  • An expression's value is lost unless it is stored or printed.
  • Comments explain code and can switch lines off while you investigate.

16AI learning prompts

Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.

Concept explanation Statements and expressions
Context: I am learning the difference between statements and expressions.
Task: Explain it with five labelled examples from a shopping-cart program.
Constraints: Use only let, const, numbers, strings and arithmetic.
Expected behaviour: I can label any simple line as statement, expression or both.
Testing: Give me five unlabeled lines to classify, with answers at the end.
Code prediction Precedence drill
Context: I know * and / come before + and -.
Task: Give me six arithmetic expressions mixing + - * / % and parentheses and ask for their values.
Constraints: Integers only, results under 200. Wait before revealing answers.
Expected behaviour: Each expression has a single numeric answer.
Testing: Explain the grouping for any I get wrong.
Documentation Review my comments
Context: I added comments to a 10-line pay calculator.
Task: Tell me which comments explain why and which only repeat what the code says.
Constraints: Do not change the code. Suggest at most three improved comments.
Expected behaviour: My comments add information the code does not show.
Testing: Quote each comment you discuss so I can find it.
Debugging Wrong average
Context: My average of three scores prints a number that is far too large.
Task: Help me find the problem by asking questions about my expression.
Constraints: Do not give me the corrected line until I have tried twice.
Expected behaviour: I find the missing parentheses myself.
Testing: Ask me to print the parts of the expression separately.
Exercise generation Expression puzzles
Context: I finished the weekly pay exercise.
Task: Create two practical calculation exercises (for example a recipe scaled for more people).
Constraints: Provide constants, expected output and one rule that needs parentheses.
Expected behaviour: Each exercise has exact expected output.
Testing: Include the expected output and a test I can run by comparing it.