Skip to the content

Commenting what matters

JavaScript · Unit 19: Clean code

A comment starts with // and never runs: it's a note for whoever reads the code. You've seen them in every // your code here.

The code already says what it does. What it can't say is why it's done that way.

// 5 is the bank's fixed fee
const fee = 5;
console.log(100 - fee);

Prints

95

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Multiple choice

    Which comment helps whoever reads this function six months from now?

  3. 3. Predict the output

    What does this code print?

  4. 4. Find the bug

    Someone switched a line off to test something and forgot to switch it back on: this should print 60. Which line has the error?

  5. 5. Complete the code

    The multi-line comment explains where the 6 comes from. Finish it so the program runs.

  6. 6. Predict the output

    What does this code print?

  7. 7. Multiple choice

    What is this block missing so the function can be used without opening it?

  8. 8. Find the bug

    The comment states the rule that has to be met, so with 18 it should print true. Which line has the error?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →