This content originally appeared on DEV Community and was authored by WISDOMUDO
Introduction
As a beginner in JavaScript, understanding your code is just as important as writing it. That’s where comments come in. JavaScript comments can be used to temporarily disable particular parts of the code during testing or to clarify what the code does and why it was written a certain manner. Although they have no impact on the code’s functionality and are invisible to the browser, they are incredibly helpful to developers.
Whether you’re writing code for yourself or working on a team, clear comments make your code easier to understand, maintain, and update over time.
In this article, we’ll cover:
- What JavaScript comments are
- How to write single-line and multi-line comments
- How comments can be used to disable parts of your code during testing
- Best practices for using comments as a beginner
What Are JavaScript Comments?
The browser ignores the lines of text that are written into your code as JavaScript comments. They’re there to help humans (like you or your future self) understand the code later. As projects grow in size and complexity, comments become crucial for maintaining clear and readable code.
JavaScript comments can be divided into two categories:
- Single-line comments
- Multi-line (or block) comments
Single-Line Comments in JavaScript
For quick explanations or notes on a specific line, single-line comments are the best choice. They begin with two forward slashes //.
Example:
// This will display 'hello wisdom' in the browser's console
console.log('hello wisdom');
let total = 4 + 3; // Add two numbers
You can also place a comment at the end of a line of code, as shown above.
Multi-Line Comments in JavaScript
If your code requires a more detailed explanation, especially when it’s intricate or does several things, multi-line comments (starting with /* and ending with */) are a great solution.
Example:
/* This block of code multiplies two numbers,
stores the result in a variable,
and prints the result to the console.
*/
let result = 4 * 3;
console.log('Multiplication result is:', result)
Multi-line comments are useful for adding detailed notes, explaining how functions work, or showing the intention behind certain parts of your code.
Using Comments to Disable Code
One useful feature of comments is that they can prevent code from running. This is helpful when you’re testing, debugging, or trying to temporarily remove a piece of code.
Disable a single line:
// console.log('This line will not run');
Disable multiple lines:
/*
let x = 30;
let y = 40;
console.log(x + y);
*/
This is known as commenting out code. It lets you test your changes without deleting anything.
Best Practices for Commenting Code
Here are some helpful tips for writing effective comments:
- Be clear and concise: Write comments that are easy to understand.
- Explain why, not just what: Focus on explaining the purpose behind the code.
- Avoid over-commenting: Don’t state the obvious. Let your code be self-explanatory where possible.
- Update comments when you update your code: Outdated comments can be misleading.
Conclusion
As a beginner, using comments is a great way to make your JavaScript code clearer and more maintainable. Whether you’re writing simple scripts or working on large projects, getting into the habit of writing helpful comments will improve your code and make collaboration easier.
You can reach out to me via LinkedIn
This content originally appeared on DEV Community and was authored by WISDOMUDO