Comments in JavaScript

Comments in JavaScript

How to comment your code properly.

Hello everyone 👋🏾 Welcome, in today’s post we’ll be learning about comments in JavaScript. We would learn how, when and where to write comments and some of the best practices when writing comments as a developer.

What is a comment?

A comment is a piece of code that is ignored by the compiler when the code is run.

How to write comments in JavaScript

There are two ways to write comments in JavaScript. Comments can be written as inline or block comments :

  1. Inline/Single-line comments: these are comments that fit on a single line. Inline/single-line comments in JavaScript are written like this: //single-line comment. JavaScript compiler ignores everything after the // symbol.

  2. Block/Multi-line comment: these are comments that span over multiple lines. How to write a block/multi-line comment in JavaScript :

    • start with /** symbol.

    • Write your comment.

    • To add a new line begin with *.

    • End with */.

Example:

// a one line comment

/* this is a normal
multi-line comment */

/** 
*  this is a standard,
*  multi-line comment
*/

NB block comments cannot be nested

How to use comments in your code

Comments are very useful in programming because they serve a variety of purposes ranging from explaining your code to debugging and much much more. Here are some of the uses of comments when writing code:

  1. Comments should be used to solve the problem before writing the actual code. From how to think like a programmer you’d learn that programming for starters programming is much less about writing code or the language you write in rather than learning the skill of thinking algorithmically. So before you write that code, first of all, break down the problem and write the logic for your solutions as comments! Here’s an example: my solution for leetcode question: time needed to buy tickets

     // 1. create a variable to keep track of index
     // 2. start from index 0
     // 3. create a variable to keep track of seconds
     /* number of iterations is unknown so a while loop
     * is suitable. the loop should continue as long as tickets[k] 
     * is greater than zero
     */
     // for every index, if ticket[index] > 0 then subtract 1 and increment seconds 
     // increment the index
     var timeRequiredToBuy = function(tickets, k) {
         // index
         let i = 0;
         // number of seconds
         let seconds = 0;
         // while tickets[k] > 0
         while (tickets[k]) {
             const index = i % tickets.length;
             if (tickets[index]) {
              // if element is not 0 then decrement the value and increment the number of steps
                 tickets[index]--;
                 steps++;
             }
             // every time move to next element
             i++;
         }
         return steps;
     };
    
  2. Comments are used to explain the purpose of a piece of code. You should use comments to explain why you wrote a piece of code and what role it place. Comments can also be used to write the concept employed when a piece of code is written. For starters, this will help solidify the newly learned concept and help in revising. For example

     let people = ['Henry', 'Francis', 'Micheal'];
     const [first, ...rest] = people; // array destructuring here.
     console.log(first) //-> 'Henry'
     console.log(rest) //-> ['Francis', 'Micheal']
    
  3. Comments are used in debugging. Sometimes to find out where problems originate from in our code we can simply comment out parts of our code and test the rest of the code. This prevents deleting and rewriting unproblematic codes multiple times.

  4. Comments can be used to tag members of a team. When working with a team to build a product you can use comments to assign tasks to other team members or draw their attention to issues.

That’s a wrap! Thank you for reading this post, I hope it was useful. Comment to let me know what you think. If you found this post helpful click the follow button for more helpful tips on using JavaScript and other technologies in web development!. Also, if you have any topics you’d like me to cover kindly let me know in the comments. Don’t forget to like and share.

Links to social media profiles: Twitter

Work with me :)