How To Write Comments in JavaScript on Your Cloud Server?


As a web developer, it can be obstructing to dive into code composed by another person that was not appropriately commented, and it's surprisingly simple to overlook what your own code implied when you're no longer inundated with regards to a program. Commenting your program or code in the early stage will strengthen excellent programming practices all through your profession to maintain a strategic distance from these kind of issues later on.

Syntax for Comment

Let us now take a glimpse of 2 different types of syntax for making comment.

All single-line comments are composed with two forward slashes (//):

// Example of a comment

All characters promptly following the //syntax until the point that the finish of the line will be overlooked by JavaScript.

Block comments, here and there alluded to as mutli-line remarks, are composed with opening tags (/*) and shutting tags (*/). On the off chance that you know CSS, at that point you're as of now acquainted with block-level comments.

/* Just a
comment */

Every character between the opening tag and closing tag will be refused.

Both single-line as well multi-line comments are composed over the code they are assigned to clarify, as exhibited in this "Welcome, to my world!" illustration:

welcome.js

// Print "Welcome, to my world!" to the console
console.log("Welcome, to my world!");

When making any new comments, imprint them at an indistinguishable level from the code instantly underneath them:

hostings.js

// Initialize a function
functionalphabetizehostings() {
   // Define hostings variable as a list of strings
   consthostings= ["Shared", "VPS", "Cloud", "Dedicated", "Reseller"];

   // Print alphabetized array to the console
   console.log(hostings.sort());
}

Keep in mind that comments are the same amount of a piece of the code as the program itself. Obsolete comments can be to a greater extent a hindrance than no comments by any stretch of the imagination, so make sure to keep up and refresh comments frequently alongside everything else.

Inline Comments in JavaScript

 

One-line comments are specified to as inline comments when they show up toward the finish of a line of code.

let a = 55;    // assign numerical value to a
let b = x + 5; // assign the sum of x + 5 to b

 

Inline comments can be easily utilized for for fast comment on little, particular pieces of content. As the comment should just identify with the correct line it's composed on, it is the most evident kind of comment.

 

Keep in mind that there is no other way to end a single line comment on a line, so make a point not to put any code after the // syntax, as found in the illustration underneath.

broken.js

for (let i = 0; i === 25; i++) // for loop that runs twenty five times {
   // Running this code results in a syntax error
}

Despite the fact that inline comments can be very helpful, they ought to be utilized sparingly - code shrouded in an abundance of inline comments will rapidly end up messy and along these lines hard to peruse.

Block Comments

Block based comments, or multi-line comments, are long-shape explanations used to present and clarify a segment of code. Normally these sorts of comments are put at the highest point of a document, or before an especially complex code square.

greet.js

/* Initialize and invoke a the greetUser function
to assign user's name to a constant and print out
a greeting. */

functiongreetUser() {
   const name = prompt("What’s your name?");
   console.log("Hi ," + person name + "! How are you doing?");
}

greetUser();

You may likewise infrequently observe a somewhat changed variant of the block comment syntax, which begins with/** and incorporates reference asterisks all through the left half of comment block.

sea.js

/**
* Initialize constant with an array of strings.
* Loop through every item in the array and print
* it to the console.
*/

const seaCreatures = ["Dragonfish", "Jellyfish", "Blobfish"];

for (const seaCreature of seaCreatures) {
 console.log(seaCreature);
}

Now and then this kind of comment will likewise incorporate insights about the programming file, including the content's name, form, and developer.

In the event that you are a novice in JavaScript, you may compose as much as important to learn and grasp the code you compose. As you advance as a JavaScript developer, you will hope to answer the goal, or the why behind the particular code, rather than the how for sure.

Writing for Code for Testing Purpose

 

Comment can likewise be utilized to rapidly and effectively prevent execution of code for testing and troubleshooting purposes. This is alluded to as "commenting out code".

In the event that there is an issue in some code you've composed, commenting out segments will keep them from running, and can be useful in pinpointing the origin of the issue. You may likewise utilize it to flip between code to test distinctive results.

math.js

// Function to sum of two numbers
functionsumTwoNumbers(a, b) {
 let sum = a + b;
 return sum;
}

// Function to multiply two numbers
functionmultiplyTwoNumbersa, b) {
 let product = a * b;
 return product;
}

/* In this illustration, we are commenting out the addTwoNumbers
function, accordingly keep from happening it from executing. Only the
multiplyTwoNumbers function will function*/


// addTwoNumbers(10, 25);
multiplyTwoNumbers(25, 10);

Two considered together single-line comment and block level comments can be utilized to comment out any code, determined by the extent of the area being toggled.