How to Add Comments in Node.js

It's import for software developers to be able to leave comments in their source code. Comments help other contributors understand the code as well as highlight important sections.

Comments are exactly what they sound like. Comments are added to lines of code and/or throughout functionality in the codebase as annotations and are ignored by the interpreter when building. Since comments are ignored, they serve an informational purpose for the contributors of a project and have no effect on the code’s functionality.

Comments are commonly used to help described what a piece of code is doing so that other contributors working in the codebase can easily digest what they’re looking at. Comments are also used to designate the need for future work within a codebase, typically with the ‘TODO:’ placed before the comment itself.

There are multiple ways to comment a file, but for now let’s go through two ways to add comments to your Node.js code.

Single Line & Inline Comments

The easiest way to add a comment to your code is to prefix your comment with two back slashes (“//”). You can add the two slashes at the start of a line and type your comment as well as appending your comment to a specific line by adding the back slashes and comment inline with the thing you’re describing.

Single Line Comment

1// This function returns the first 85 characters of a string 2const slice = (string: string) => { 3 return`${string.slice(0, 85)}`; 4};

Inline Comment

1const slice = (string: string) => { 2 return`${string.slice(0, 85) 3}`; // This function returns the first 85 characters of a string };

Block Comments

Block comments are typically used when the commentor needs to specify a lot of information in a single comment. Block comments are created by starting your comment with ‘/', typing your comment across multiple lines, and ending your comment with '/’. Block comments are also benficial when the commentor needs to specify a list of items such as TODOs.

Block Comment

1/* 2 3TODOs 4 5- Type check the input string 6 7- Check if string has more than 85 characters 8 9- Remove spaces before returning the new string 10 11*/ 12 13const slice = (string: string) => { 14 return`${string.slice(0, 85)}`; 15};

Thanks for reading!