Comments are notes you write in your code. The computer ignores them. Multi-line comments are for writing notes that take up more than one line. They help you explain complex parts of your code to yourself and to other programmers.
Multi-line Comments (/* */)
Multi-line Comments (/* */)
In JavaScript, a multi-line comment is a special type of note. It starts with /* and ends with */. Everything between these two markers is a comment. The computer will completely ignore it when running your program.
Multi-line comments are perfect for writing longer explanations. They are also useful for temporarily turning off a block of code while you are testing or debugging your program.
What is a Multi-line Comment?
A multi-line comment in JavaScript starts with /* and ends with */. It allows you to write notes or explanations that span across several lines in your code.
Long Explanations
Use multi-line comments for detailed notes about how a complex part of your code works.
Temporarily Disable Code
You can wrap code in /* */ to stop it from running without deleting it.
Code Documentation
They help explain what a function or a group of code does for future reference.
Teamwork
Comments make your code easier for other people to understand and work with.
How to Write Multi-line Comments
How to Write Multi-line Comments
Writing a multi-line comment is very simple. You just type /* to start the comment. Then you type your notes on as many lines as you need. Finally, you type */ to tell JavaScript where the comment ends.
Everything between /* and */ is ignored. It does not matter if it is text, numbers, or even JavaScript code. The computer will simply skip over it.
1/*2 This is a multi-line comment.3 It can span many lines.4 It helps explain the code below.5*/67let score = 100; // This is a single-line comment8let playerName = 'Hero';910/*11 The 'score' variable keeps track of the player's points.12 The 'playerName' variable stores the player's name.13 We will use these variables in our game logic later.14*/1516console.log(playerName + ' has ' + score + ' points.');
Do not forget to close your comment!
If you start a multi-line comment with /* but forget to close it with */, JavaScript will think all the rest of your code is part of the comment. This will cause your program to stop working and show errors. Always make sure to close your comments!
Why Use Multi-line Comments?
Why Use Multi-line Comments?
Multi-line comments are best when you have a lot to say about your code. Imagine explaining a complex math calculation or a tricky game rule. A single-line comment might not be enough space.
They also let you temporarily hide code. If you are trying to find a bug, you can 'comment out' a section of code. This means you wrap it in /* */ so it does not run. This helps you narrow down where the problem is.
1/*2 This function calculates the total price of items in a shopping cart.3 It takes an array of item objects, each with 'price' and 'quantity'.4 It also applies a 10% discount if the total is over $100.5*/6function calculateTotalPrice(items) {7 let total = 0;8 for (let i = 0; i < items.length; i++) {9 total += items[i].price * items[i].quantity;10 }1112 /*13 // Temporarily disabling the discount logic for testing.14 // If total is over 100, apply a 10% discount.15 if (total > 100) {16 total = total * 0.9; // 10% off17 }18 */1920 return total;21}2223let cartItems = [24 { name: 'Shirt', price: 25, quantity: 2 },25 { name: 'Pants', price: 40, quantity: 1 }26];2728let finalPrice = calculateTotalPrice(cartItems);29console.log('Final Price:', finalPrice); // Should be 90 (50 + 40)
// This is a very long explanation// about why this specific function// needs to handle error cases in a// special way. It checks for null// values and also empty strings// before processing the input data.function processData(data) { /* ... */ }
/*This function handles error cases in a special way.It checks for null values and also empty stringsbefore processing the input data to prevent crashes.*/function processData(data) { /* ... */ }
Multi-line vs. Single-line Comments
Multi-line vs. Single-line Comments
JavaScript has two main ways to write comments: multi-line comments (/* */) and single-line comments (//). Both are used to add notes to your code, but they are for different situations.
Single-line comments (//) are for short notes. They start with // and go to the end of that one line. Multi-line comments (/* */) are for longer notes that need more space, or for turning off a whole block of code.
// This variable stores the user's age.let userAge = 30;// Check if the user is old enough.if (userAge >= 18) {// Log a message to the console.console.log('User is adult.');}
/*The 'userAge' variable is crucial for age-gated content.It must be updated carefully after user verification.*/let userAge = 30;/*This entire block can be commented outfor testing purposes if we want to bypassthe age check temporarily.if (userAge >= 18) {console.log('User is adult.');}*/
// This variable stores the user's age.let userAge = 30;// Check if the user is old enough.if (userAge >= 18) {// Log a message to the console.console.log('User is adult.');}
/*The 'userAge' variable is crucial for age-gated content.It must be updated carefully after user verification.*/let userAge = 30;/*This entire block can be commented outfor testing purposes if we want to bypassthe age check temporarily.if (userAge >= 18) {console.log('User is adult.');}*/
When to choose which comment type
Use // for quick, short notes on a single line. Use /* */ for longer explanations, file headers, or to comment out large chunks of code.
Using Multi-line Comments in Real Projects
Using Multi-line Comments in Real Projects
In real-world coding, multi-line comments are not just for simple notes. They are often used for important tasks like documenting a large function or marking a section of code for review. They help keep big projects organized and understandable.
They also play a big role in debugging. When you are trying to fix a problem, you might want to turn off certain parts of your code to see if the error goes away. Multi-line comments make this easy and safe.
Document a complex function
Add a multi-line comment before a function to explain what it does, what inputs it needs, and what it returns.
1/*2 * This function processes user input from a form.3 * It validates the 'username' and 'email' fields.4 * Returns true if validation passes, false otherwise.5 */6function validateForm(username, email) {7 // ... validation logic ...8 return true;9}
Temporarily disable a code block
Wrap a section of code in /* */ to prevent it from running. This is useful for testing. Remember to remove or uncomment it later.
1let data = fetchData();23/*4// This section was causing an error.5// I'm commenting it out to isolate the problem.6if (data.status === 'error') {7 displayErrorMessage(data.message);8 return;9}10*/1112processData(data);
Add a file header comment
Many projects start each file with a multi-line comment. This comment contains information like the file's purpose, author, and creation date.
1/*2 * File: app.js3 * Author: Jane Doe4 * Date: October 26, 20235 * Description: Main application logic for the website.6 * Contains functions for user authentication and data display.7 */89// Start of actual code10console.log('Application started.');
You cannot nest multi-line comments
JavaScript does not allow you to put one multi-line comment inside another multi-line comment. If you try to do /* outer /* inner */ outer */, the first */ will close the 'outer' comment, and the rest will cause an error.
Comment Syntax Reference
Comment Syntax Reference
| Comment Type | Syntax | Use Case | Can Span Lines? |
|---|---|---|---|
| Multi-line | `/* ... */` | Long explanations, disabling code blocks, file headers | Yes |
| Single-line | `// ...` | Short notes, commenting out a single line of code | No (ends at line break) |
Test Your Knowledge
Test Your Knowledge
Which of these is a correctly written multi-line comment in JavaScript?
What happens if you start a multi-line comment with /* but forget to add */?
When is it best to use a multi-line comment (/* */) instead of a single-line comment (//)?
Which of these is a benefit of using multi-line comments in your code?
Quick Reference
Quick Reference
- 1Purpose — Multi-line comments (
/* */) are for notes that span multiple lines. - 2Start and End — They begin with
/*and must end with*/. - 3Ignored by Computer — JavaScript completely skips over comments when running code.
- 4Long Explanations — Use them to provide detailed explanations for complex code sections.
- 5Disable Code — You can wrap a block of code in
/* */to temporarily turn it off. - 6Debugging Aid — Commenting out code helps find bugs by isolating problematic sections.
- 7File Headers — Often used at the top of a file to describe its purpose, author, and date.
- 8Readability — Good comments make your code easier for others (and your future self) to understand.
- 9No Nesting — You cannot put one multi-line comment inside another multi-line comment.
- 10Error if Unclosed — Forgetting
*/will cause JavaScript to treat subsequent code as a comment, leading to errors.
Single-line Comments
Learn about // for quick, one-line notes in your JavaScript code.
Debugging Techniques
Explore other ways to find and fix errors in your code, like using console.log().
Code Style Guides
Understand best practices for writing clean, readable, and consistent code.
JSDoc Comments
Discover a special type of multi-line comment for generating code documentation.
You now understand Multi-line Comments!
You have learned how to use /* */ for multi-line comments in JavaScript. You know when to use them for long explanations, disabling code, and improving your code's clarity. Keep practicing to make your code easy to understand!
Try it in the Javascript Compiler
Run and experiment with Javascript code right in your browser — no setup needed.