×

Please try us on a desktop

Our app relies on some desktop browser features to render design files and interactable code. Unfortunately, we haven't been able to make these work on mobile yet.

Please try Kombai using a chromium based browser on desktop.

JavaScript

How to comment in HTML, CSS and JavaScript?

Published on: April 23, 2023

Comments are of great help when used properly in moderation. It allows developers to communicate their intentions to other team members, making it easier for them to understand and contribute to the code.

In this article, we will explore how to comment in HTML, JavaScript, and CSS and best practices for commenting.

How to Comment in HTML?

Comments in HTML are typically used to describe the structure of a document, provide additional information, or disable specific portions of the code. Let's take a look at how to comment in HTML using single-line and multiline comments.

1. Single-line Comment in HTML

Comments in HTML are created using <!-- and -->. There is no specific syntax for single-line comments in HTML. However, you can create a single-line comment using the comment syntax by placing your comment within the same line.

<body>
  <div>
    <!-- This is a single-line comment in HTML -->
  </div>
</body>

2. Multiline Comment in HTML

To add Multiline comments in HTML, you have to add the comment markers in two different lines and add the stuff you want to comment in between. Whatever you write between these two markers will be ignored by the browser.

<body>
  <div>
    <!--
            This is a multiline comment in HTML.
            It can span multiple lines.
        -->
  </div>
</body>

How to Comment in CSS?

CSS comments are useful for describing the purpose of specific style rules and for organizing your stylesheets. Let's learn how to comment in CSS using single-line and multiline comments.

1. Single-line Comment in CSS

Just like HTML, there is no specific syntax for single-line comments in CSS. You can use the comment markers /* and */ in the same line to create a single-line comment.

/* This is a single-line comment in CSS */

2. Multiline Comment in CSS

To add multiline comments in CSS, place both the comment markers /* and */ in two different lines. Whatever you write between them will be ignored by the browser.

/*
  This is a multiline comment in CSS.
  It can span multiple lines.
*/

How to Comment in JavaScript?

JavaScript comments are essential for explaining the purpose of your code, especially when working with complex logic or algorithms.

They can also be used to disable specific lines of code for debugging purposes. Let's see how to comment in JavaScript using single-line and multiline comments.

1. Single-line Comment in JavaScript

Single-line comments in JavaScript are created using the // syntax. The browser will ignore any text that follows the double forward slashes until the end of the line.

// This is a single-line comment in JavaScript

2. Multiline Comment in JavaScript

Multiline comments in JavaScript are created using the /* and */ syntax, same as CSS. Browser will ignore anything that you write between these two markers.

/*
  This is a multiline comment in JavaScript.
  It can span multiple lines.
*/

Best Practices for Commenting

Here are a few guidelines that can help you write clear and useful comments in HTML, JavaScript, and CSS:

  • Be concise: Keep your comments brief and to the point. Use clear and concise language to convey the purpose of the code or the reason for a specific implementation.

  • Use proper grammar and spelling: Take the time to proofread your comments and ensure that they are free of grammar and spelling errors.

  • Comment on the why, not the what: It's often more valuable to explain why a particular piece of code exists or why you made a certain decision, rather than describing what the code does. The code itself should be self-explanatory in most cases.

  • Update comments when you update the code: If you make changes to your code, be sure to update the corresponding comments as well. Outdated comments can be confusing and may lead to incorrect assumptions about the code's functionality.

  • Don't over-comment: Avoid the temptation to comment on every single line of code. Focus on commenting areas that are complex or might be difficult to understand at a glance.

Conclusion

Now that the article has come to the end, we hope that you have learned not only how to write good comments but also why and when you should write them.

Remember that comments should be used judiciously and should always be up-to-date and accurate. Always ensure that your comments are both useful and informative, making your code easier to work with for both yourself and your team members.

Happy Coding!