JavaScript Block Comment: How to Comment Multiple Lines

JavaScript multiline comments, also known as block comments, start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). 

Multiple line comments are used to prevent the execution of many lines of code.

Syntax

/* This is
   Multiple line or block
   Comment
*/

Example

/* 
 We can write anything here.
 console.log("this is multiple line or block comment");
 function calculator(){
 // some code
}
*/
console.log('Avengers the multiline');

Output

Avengers the multiline

Why do we need to use multiple lines or block comments in JavaScript

A comment is usually used to explain the code to the developers and comment to create a more readable code.

If we have one function, but for some reason, we need to stop that execution for a while, we comment on that function. So that function does not execute. 

Multiple Line or Block Comments are also used for formal documentation.

Best use of multiple lines or block comment

/*

## getMovieList function ##

suggetion :- In the fetch function use constant url instead of custom.
explain : - In this function we get our movieList by using our api url.

*/
async function getMovieList() {
   try {
       const res = await fetch("https://movie.com/api/movielist");
       if (res.status === 1) {
            return res.data;
       } else {
            throw res;
       }
   } catch (err) {
     console.log(err.message || "something went wrong !");
  }
}

In the above example, we create multiple lines or block comments, and inside that block, we suggest and explain our getMovieList() function.

1 thought on “JavaScript Block Comment: How to Comment Multiple Lines”

Comments are closed.