basiccodingconcept.online

What Are Comments?

Comments are messages that explain code and tell humans why it works; code tells the computer what to do, while Comments work the same way as notes beside an actual recipe. Imagine Mom’s cooking recipe where each ingredient and step is an instruction and writing little notes can help programmers understand one part, skip confusion, or add context without changing the actual code.

These messages can tell a later human why a secret method or choice come from testing done weeks or months earlier. Clear comments are valuable because they help people come back and understand the same code better without changing how the computer works.

What Are the Types of Comments?

Comments explain complex logic in code without changing program behavior.

Single Line Comments

 A single-line comment creates text with two forward slashes (//), and // appears after the comment marker at the start of a line or adds a note at the end line. Example:

JavaScript

// Calculate user scores

let scores = 25;

Multi Line Comments

A multi-line comment uses /* and */, and a multi-line comment can span multiple lines across sections of an entire file. Example:

JavaScript

/* Calculate user scores

   Fix logic later

   Explain age checks */

let age = 25;
02 · Comments — JavaScript Playground
02

Comments

Comments explain your code without running it. Use // for one line and /* */ for many.

JavaScript
Output

    

 

When to Use Comments

  • Comments Explain complex logic and thinking that are not obvious, so the Document records the reason behind the code.
  • Comments Describe what a function does and what it expects as input, which explains program behavior more clearly.
  • TODO notes Mark things that need a fix, so developers can come back and complete unfinished tasks.
  • Comments can Explain discount business rules that apply only Tuesdays, which records specific conditions directly beside the related code.
  • Leave a comment when code can Temporarily disable debugging, because the note identifies the purpose of that change.

When NOT to Comment?

  • Do NOT write a Comment that only states the obvious, because the code already shows the same action.
  • Keep comments concise and relevant, and avoid novels that repeat code instead of adding useful context.
  • Do NOT leave outdated comments, because they no longer match the current code after a change.
  • Update comments when you change code, so the description matches the current implementation.
  • If the comment count is set to zero or 0, let the code communicate simple behavior without extra comments where the meaning is already obvious.

How Do Comments Work in Different Languages?

  • A comment explains programming code and does not affect program execution. Different languages use different syntax for single line and multi-line comments.
  • JavaScript use // single line comments and /* **multi-line** */ comments for longer notes.
  • Java use // single line comments and /* **multi-line** */ comments with the same syntax style.
  • C++ use // single line comments and /* **multi-line** */ comments to document code.
  • Python use # single line comments and “”” **multi-line** “”” strings as multi-line documentation in many code examples.
  • HTML use <!– **comment** –>, while CSS use /* **comment** */, showing that comment syntax varies across different languages.

How Do Different Types of Comments Work?

Commenting includes notes that explain Code without changing program behavior. Commenting out code is a technique that means turning a comment into existing code so it won’t run temporarily. This method is helpful for debugging, because developers can disable a discount or another function while testing. One example uses a function named calculateTotal.

JavaScript

function calculateTotal(items) {

  let total = 0;

  for (const item of items) {

    total += item.price;

  }

  // total = total \ 0.9;

  return total;

}

In this example, total = 0 starts the calculation, and item.price values add to total with +=. The disabled line represents total = total * 0.9, which applies a discount when active. During debugging, commenting that line can disable the discount, and the return statement returns total.

Comments Quiz

Comments Quiz

Understand how and why programmers use comments in code.

Question 1 of 10 Score: 0
0%

What are the Key Takeaways?

  • Comments are notes for humans, and computers ignore Comments completely.
  • JavaScript Uses // for single-line comments, and Uses / / for multi-line comments.
  • Comments explain why, not what, because obvious code often shows what the code does.
  • Keep Comments up to date when changes affect code, because code changes can make old notes inaccurate.
  • Good Comments add clear context, include a relevant date when needed, and avoid over-comment practices that repeat obvious code.