Engineer bro!

Sat Apr 09 2022 (1 month ago)

Engineer by mistake!

I am a software engineer by passion

Comments in JavaScript

Commenting involves placing Human Readable Descriptions inside of computer programs detailing what the Code is doing. Proper use of commenting can make code maintenance much easier, as well as help make finding bugs faster. Further, commenting is very important when writing functions that other people will use.

As time goes on, programs become more and more complex. It becomes necessary to add comments which describe what the code does and why.

Comments can be put into any place of a script. They don’t affect its execution because the engine simply ignores them.

Types of comments in JavaScript

There are two types of comments

  • Single line comment - starts with //

  • Multiline comment - starts with /* and ends with */

Single line

// This comment occupies a line of its own
alert('Hello');

alert('World'); // This comment follows the statement

Multiline comments start with a forward slash and an asterisk /* and end with an asterisk and a forward slash */.

/* An example with two messages.
This is a multiline comment.
*/
alert('Hello');
alert('World');

The content of comments is ignored, so if we put code inside /* … */, it won’t execute.

Sometimes it can be handy to temporarily disable a part of code:

/* Commenting out the code
alert('Hello');
*/
alert('World');

Why we should add comments in the code?

  • Time-Saving - It saves our time when we come back to see the old code.

  • Keep track of what needs to be done - By providing comments within your code, you can easily find the next tasks to be done within your software.

  • Alternative methods - We sometimes comment out large pieces of code that act as an alternative method to the actual method that works, which is being used. By doing this, I can figure out and debug my code extensively by understanding which method worked and which method did not work.

Comments do not affect the performance of any software at all. What comment does is it provides a way for you to understand, in human terms, what is going on within your code. To me, the positives outweigh the negatives in this case so guys and girls, let’s just start commenting on our codes.

EngineeringSoftware EngineeringJavaScript

Comments (0)

Discussions (0)


© 2021 dsabyte. All rights reserved