codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

4 / 65 topics
1JavaScript Introduction2JavaScript console.log() & Output3JavaScript Syntax & Statements4JavaScript Comments5JavaScript Variables (var, let, const)6JavaScript Data Types7JavaScript Operators8JavaScript Type Conversion
Tutorials/JavaScript/JavaScript Comments
🌐JavaScript

JavaScript Comments

Updated 2026-05-12
8 min read

JavaScript Comments

In this tutorial, you'll learn about the different types of comments available in JavaScript and how to use them to make your code more readable and maintainable. Understanding comments is crucial for documenting your code effectively, especially as projects grow in complexity.

Introduction

Comments are lines or blocks of text that are ignored by the JavaScript engine when executing a script. They serve several purposes:

  • Documentation: Explain what a piece of code does.
  • Disabling Code: Temporarily remove code from execution without deleting it.
  • Debugging: Help identify issues in your code.

JavaScript supports two types of comments: single-line and multi-line. In this section, we'll explore both types and discuss best practices for using them effectively.

Single-Line Comments

A single-line comment is used to add a brief note or explanation on the same line as your code. It starts with // and continues until the end of the line.

Example 1: Basic Single-Line Comment

single-line-comment.js
1// This is a single-line comment
2let x = 5; // Initialize variable x to 5
3console.log(x); // Output the value of x
Terminal
$ node single-line-comment.js
5

In this example, comments are used to explain what each line of code does. This makes it easier for others (and yourself) to understand the purpose and functionality of the code.

Multi-Line Comments

Multi-line comments allow you to add longer explanations or disable multiple lines of code at once. They start with /* and end with */.

Example 2: Basic Multi-Line Comment

multi-line-comment.js
1/*
2This is a multi-line comment.
3It can span multiple lines
4and explain more complex logic.
5*/
6let y = 10; // Initialize variable y to 10
7console.log(y); // Output the value of y
Terminal
$ node multi-line-comment.js
10

In this example, a multi-line comment is used to provide more detailed information about the code block. This is particularly useful for explaining complex algorithms or sections of code that require additional context.

Best Practices for Commenting

While comments are powerful tools, it's important to use them judiciously and effectively. Here are some best practices:

  1. Explain Why, Not What: Comments should explain the reasoning behind a piece of code rather than just repeating what the code does.
JavaScript
1// Bad practice: This comment is redundant
2 let z = 20; // Initialize variable z to 20
3
4 // Good practice: Explain why this value is used
5 const TAX_RATE = 0.08; // Standard tax rate for sales in the state
  1. Keep Comments Up-to-Date: Outdated comments can lead to confusion and misunderstandings. Always update comments when you modify the code.

  2. Use Multi-Line Comments Sparingly: Overusing multi-line comments can make your code cluttered. Use them only when necessary, such as for complex algorithms or large blocks of disabled code.

  3. Avoid Commenting Out Code: Instead of commenting out code, consider using version control systems like Git to manage different versions of your code.

  4. Document Functions and Classes: Use comments to describe the purpose, parameters, and return values of functions and classes.

JavaScript
1/**
2 * Calculates the total price after applying tax.
3 * @param {number} price - The original price before tax.
4 * @returns {number} - The total price including tax.
5 */
6 function calculateTotal(price) {
7 const tax = price * TAX_RATE;
8 return price + tax;
9 }
  1. Use JSDoc for Documentation: For larger projects, consider using tools like JSDoc to generate comprehensive documentation from your comments.

Practical Example

Let's create a simple program that calculates the total cost of items after applying a discount and tax. We'll use comments to explain each part of the code.

practical-example.js
1// Define constants for discount rate and tax rate
2const DISCOUNT_RATE = 0.1; // 10% discount
3const TAX_RATE = 0.08; // 8% tax
4
5/**
6* Calculates the discounted price.
7* @param {number} originalPrice - The original price of the item.
8* @returns {number} - The price after applying the discount.
9*/
10function applyDiscount(originalPrice) {
11 const discountAmount = originalPrice * DISCOUNT_RATE;
12 return originalPrice - discountAmount;
13}
14
15/**
16* Calculates the total price including tax.
17* @param {number} discountedPrice - The price after discount.
18* @returns {number} - The total price including tax.
19*/
20function calculateTotal(discountedPrice) {
21 const taxAmount = discountedPrice * TAX_RATE;
22 return discountedPrice + taxAmount;
23}
24
25// Example usage
26let itemPrice = 100; // Original price of the item
27let finalPrice = calculateTotal(applyDiscount(itemPrice));
28
29console.log(`The final price after discount and tax is: $${finalPrice.toFixed(2)}`);
Terminal
$ node practical-example.js
The final price after discount and tax is: $97.20

In this example, we've used comments to explain the purpose of each function and the constants used in the program. This makes it easier for others (and yourself) to understand how the code works.

Summary

  • Single-line comments start with // and are useful for brief notes on the same line as your code.
  • Multi-line comments start with /* and end with */, allowing you to add longer explanations or disable multiple lines of code.
  • Best practices include explaining why, not what, commenting out code sparingly, and documenting functions and classes.

By following these guidelines, you can write JavaScript code that is not only functional but also well-documented and easy to maintain.

What's Next?

Now that you've learned about comments in JavaScript, the next topic will cover variables. Variables are essential for storing data and are used extensively throughout your programs. In the next tutorial, we'll explore different types of variables (var, let, and const) and their use cases. Stay tuned!


This comprehensive guide should help you understand how to effectively use comments in JavaScript to improve code readability and maintainability. Happy coding!


PreviousJavaScript Syntax & StatementsNext JavaScript Variables (var, let, const)

Recommended Gear

JavaScript Syntax & StatementsJavaScript Variables (var, let, const)