Comments
Learn how to write comments to explain your code and make it easier to understand.
Comments are notes you write in your code to explain what it does. They're for humans to read—the computer completely ignores them!
What Are Comments?
Imagine you're writing a recipe and you add little notes like "Mom's secret ingredient!" or "Don't skip this step!" Those notes help you (and others) understand the recipe better, but they're not part of the actual cooking instructions.
Comments in code work the same way. They're messages to yourself and other programmers that explain what the code does and why it works that way.
Why Comments Matter
Code tells the computer what to do. Comments tell humans why you did it that way. This is incredibly valuable when you come back to your code weeks or months later!
Single-Line Comments
In JavaScript, you create a single-line comment with two forward slashes (//).
Everything after // on that line is a comment.
// This entire line is a comment
let age = 25; // You can also add comments at the end of a line
// Comments can explain complex logic
// or remind you of things to fix later Multi-Line Comments
For longer comments that span multiple lines, use /* to start and
*/ to end:
/*
This is a multi-line comment.
It can span as many lines as you need.
Great for explaining complex sections of code!
*/
/*
Author: Alex
Date: January 2024
Purpose: Calculate user scores
*/ Try It Yourself
When to Use Comments
Good Uses for Comments
- Explain complex logic: When code isn't obvious, explain your thinking
- Document functions: Describe what a function does and what it expects
- Leave TODO notes: Mark things you need to come back and fix
- Explain business rules: Why does the discount apply only on Tuesdays?
- Temporarily disable code: Comment out code while debugging
✓ Good Comment Examples
// TODO: Add error handling for invalid input
// Apply 20% discount for premium members (business rule from marketing)
let discount = isPremium ? 0.2 : 0;
/*
Binary search algorithm
Assumes the array is already sorted
Returns -1 if not found
*/
function binarySearch(arr, target) {
// ...
} When NOT to Comment
- Don't state the obvious:
let count = 0; // set count to zero - Don't write novels: Keep comments concise and relevant
- Don't leave outdated comments: Update comments when you change code
✗ Bad Comment Examples
// This increments i (obvious!)
i++;
// Add one to x
x = x + 1;
// Loop through array (the code already says this)
for (let item of items) {
// ...
} Comments in Other Languages
Different programming languages use different comment syntax:
// single line/* multi-line */ # single line""" multi-line """ <!-- comment --> /* comment */ Commenting Out Code
One useful technique is "commenting out" code—temporarily turning code into a comment so it won't run. This is helpful when debugging:
function calculateTotal(items) {
let total = 0;
for (let item of items) {
total += item.price;
}
// Temporarily disable discount while debugging
// total = total * 0.9;
return total;
} Quick Quiz
Key Takeaways
- Comments are notes for humans—computers ignore them completely
- Use
//for single-line comments in JavaScript - Use
/* */for multi-line comments - Good comments explain why, not what
- Keep comments up to date when you change code
- Don't over-comment obvious code
What's Next?
Now that you know how to document your code, let's dive deeper into working with strings—text data that powers everything from usernames to messages!
Finished this concept?
Mark it complete to track your progress