Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What is the difference between comments /*...*/ and /**...*/ in JavaScript?
In JavaScript, both /*...*/ and /**...*/ create multi-line comments, but they serve different purposes. The key difference is that /**...*/ is specifically used for documentation comments (JSDoc), while /*...*/ is for regular multi-line comments.
Regular Multi-line Comments (/*...*/)
Standard multi-line comments are used for general code documentation and explanations:
/*
* This is a regular multi-line comment
* Used for general code explanations
* Similar to C-style comments
*/
function calculateArea(width, height) {
return width * height;
}
JSDoc Documentation Comments (/**...*/)
JSDoc comments start with /** and are used to generate automatic documentation. They include special tags like @param, @returns, and @description:
/**
* Calculates the area of a rectangle
* @param {number} width - The width of the rectangle
* @param {number} height - The height of the rectangle
* @returns {number} The calculated area
*/
function calculateArea(width, height) {
return width * height;
}
console.log(calculateArea(5, 3));
15
Example in HTML Context
<script>
/*
* Regular comment for internal notes
* Not used for documentation generation
*/
/**
* JSDoc comment for API documentation
* @function greetUser
* @param {string} name - User's name
*/
function greetUser(name) {
alert("Hello, " + name + "!");
}
greetUser("JavaScript Developer");
</script>
Comparison
| Comment Type | Syntax | Purpose | Tool Support |
|---|---|---|---|
| Regular | /*...*/ |
General explanations | No special processing |
| JSDoc | /**...*/ |
API documentation | Documentation generators |
Conclusion
Use /*...*/ for regular code comments and /**...*/ for JSDoc documentation that can be processed by documentation tools. JSDoc comments help create professional API documentation automatically.
Advertisements
