Comments in JavaScript

Comments in JavaScript

·

3 min read

Hey🙋🏼‍♂️ everyone

sinchan-hello.gif (498Ă—498)

This is Aryan Sharma's blog, and I hope you're all doing well & great.

Welcome to another blog of this JavaScript course.

📍You can find my blogs here↗️

Let's dive in!

Comments

Comments can be of two types-

Single-line //

Multi-line /* ...*/

Your mind might come up with this argument:

Comments don't affect the output of our code, but still, we use them, why🤔?

Why do we use Comments in our code?

  • Gives more information about the working of a particular line of code.

  • To maintain code reliability.

  • To make code understandable to others.

  • Helps with ease of Debugging.

  • Supports multiple people to work together on code.

  • Code Review gets better.

  • Comments help beginners to learn more about code.

Let's discuss more about the specifics of comments and its various types:

Single-Line Comments:

Single-line comments are used to comment out a single line of code or to add comments on the same line as the code.

In JavaScript, two forward slashes (//) denote single-line comments:

Aryan's code// This is a single-line comment in JavaScript

⚠️ It might differ for particular different languages.

In Python,# is used to comment.

Multiline Comments:

Multiline comments are used to comment out multiple lines of code or to create comments that span multiple lines.

In JavaScript, multiline comments are enclosed within /* and */:

/*
This is a
multiline comment
*/

While it looks so simple and easy to use, people still make wrong comments and use it incorrectly.

Let's understand the difference between Good and Bad comments.

Bad comments

Novices tend to use comments to explain “what is going on in the code”.

Let's understand:

// This code will start from taking values of name and age
// Now alert pop-up will be shown featuring values of name and age.
let name = "Aryan";
let age = 18;

alert(` My name is ${name} and i am ${age} years old `);

You people might be thinking What's wrong with this code, we are calling it a bad comment...

It is said that “if the code is so unclear that it requires a comment, then maybe it should be rewritten instead”.

What you can include is how a particular block of code, an API, or a function is working, or something a person gets difficult to get about it!

Your function, or your code should be smart enough so that people could understand easily.

Now, see about the characteristics of a Good comment:-

Good comments

So, we were taught that explanatory comments are usually bad. Then which comments are good?

Describe the architecture/ workflow of your product. And also define the function that might be not understanding to others.

/**
 * Returns x raised to the n-th power.
 *
 * @param {number} x The number to raise.
 * @param {number} n The power, must be a natural number.
 * @return {number} x raised to the n-th power.
 */
function pow(x, n) {
  ...
}

Such comments allow us to understand the purpose of the function and use it the right way without looking in its code.

By the way, many editors like WebStorm can understand them as well and use them to provide autocomplete and some automatic code-checking.

so, here we have some last crux notes for this blog

Comment this:

  • The overall architecture, high-level view.

  • Function or a block of code usage.

Avoid comments:

  • That tells “how code works” and “what it does”.

  • Put them in only if it’s impossible to make the code so simple and self-descriptive that it doesn’t require them.

I hope you loved this blog
Then don't forget to follow this blog and like and share your view whether you get to know something new from this or not. Literally, it can make my day!!

See you in the next blog...Stay tuned🎶

Don't forget to follow me on:
Twitter And LinkedIn

Â