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.
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.
The ECMAScript grammar distinguishes Statements (declarations, control flow, expression statements) from Expressions, which evaluate to values. An expression statement evaluates its expression and discards the result unless it is used.
Operator precedence and associativity determine how an expression is grouped before evaluation: * is right-associative, / % bind tighter than + -, comparison operators bind looser still. Operands are evaluated left to right after grouping.
Semicolons are optional in many places because of Automatic Semicolon Insertion (ASI), but ASI has edge cases (a line starting with (, [ or a template literal continues the previous line). Many teams therefore use semicolons consistently or rely on a formatter.
Comments are stripped by the parser and have no runtime effect. Prefer comments that explain why code exists; well-named variables usually explain what it does.
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.
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.
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.
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.
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
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.
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 explanationStatements 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 predictionPrecedence 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.
DocumentationReview 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.
DebuggingWrong 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 generationExpression 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.