To comment multiple lines in JavaScript, use the /* */. Multiple line comments are used to prevent the execution of many lines of code. 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 (*/).
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
In the above example, we commented that some multiple line codes would not be executed. Here, it can print only the last console.log others will be ignored by the compiler.
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. In the api side it will return status and data so based on that we return data or display the error.
*/
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 give a suggestion and explanation for our getMovieList() function.
Why do we need to use the block comments?
So in our case, this function has minimal functionality, but there are many lines of function in production. So if a new developer goes through this file without comment, it becomes hard to understand the code. But if we write multiple line or single-line comments, it becomes easier to understand the code.
For this, we compare two examples here.
Example
const a = getList();
const b = getPopular(a);
const c = getRating(a);
Example 2
/*
getList() return a list of movies.
getPopular() method returns the popular song of that passed movieList.
getRating() method returns a best rating based movie.
*/
const a = getList();
const b = getPopular(a);
const c = getRating(a);
In the above example 1, we don’t understand what those methods will return. But in example 2, we clearly understand what that method does because of multiple lines or block comments.
That’s it for this article.
Related posts
How to Write Comment in JavaScript

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.
You are a very clever individual!