🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Comments

AI & DATA SCIENCE // comments

Comments are non-executed text used to explain code. JavaScript supports single-line (//) and multi-line (/* */) comments.

Syntax

// Single-line comment
/* Multi-line
   comment */
/** JSDoc comment
 * @param {string} name
 */

Deep Dive Course

Comments help developers understand code intent. **Single-line** comments use `//`. **Multi-line** comments use `/* ... */`. **JSDoc** comments (`/** ... */`) are used by documentation generators and IDEs for type hints.

1Understanding Comments

Comments help developers understand code intent. Single-line comments use //. Multi-line comments use /* ... */. JSDoc comments (/** ... */) are used by documentation generators and IDEs for type hints.

💡

Write comments to explain WHY, not WHAT. The code itself should be readable enough to show what it does.

editor.html
// This calculates the compound interest
function compound(principal, rate, years) {
  /* Formula: P * (1 + r)^t
     Used for savings/loan calculations */
  return principal * Math.pow(1 + rate, years);
}

console.log(compound(1000, 0.05, 10));
localhost:3000

2Practical Example

Here is a real-world application of Comments showing how it is used in production JavaScript code.

editor.html
/**
 * Greet a user by name.
 * @param {string} name - The user's name
 * @returns {string} The greeting message
 */
function greet(name) {
  return `Hello, ${name}!`;
}
console.log(greet('Alice'));
localhost:3000

3Best Practices

Follow these guidelines when working with Comments:

1. Comment complex algorithms and business logic

2. Use JSDoc for public APIs and library functions

3. Remove dead code instead of commenting it out

⚠️

Tip: Write comments to explain WHY, not WHAT. The code itself should be readable enough to show what it does.

editor.html
// This calculates the compound interest
function compound(principal, rate, years) {
  /* Formula: P * (1 + r)^t
     Used for savings/loan calculations */
  return principal * Math.pow(1 + rate, years);
}

console.log(compound(1000, 0.05, 10));
localhost:3000

Examples

Example 01Basic Usage
// This calculates the compound interest
function compound(principal, rate, years) {
  /* Formula: P * (1 + r)^t
     Used for savings/loan calculations */
  return principal * Math.pow(1 + rate, years);
}

console.log(compound(1000, 0.05, 10));
Example 02Advanced Example
/**
 * Greet a user by name.
 * @param {string} name - The user's name
 * @returns {string} The greeting message
 */
function greet(name) {
  return `Hello, ${name}!`;
}
console.log(greet('Alice'));

Best Practices

  • Comment complex algorithms and business logic
  • Use JSDoc for public APIs and library functions
  • Remove dead code instead of commenting it out

Interview Question

What is JSDoc and why is it useful?

Hint: Think about IDE tooling and documentation generation.

JSDoc is a documentation standard for JavaScript using special comment syntax. IDEs use JSDoc annotations to provide autocomplete, type checking, and inline documentation. Tools like TypeDoc can generate HTML documentation from JSDoc comments.

Exercises

MediumPractice using Comments in a real scenario.
View Solution
// This calculates the compound interest
function compound(principal, rate, years) {
  /* Formula: P * (1 + r)^t
     Used for savings/loan calculations */
  return principal * Math.pow(1 + rate, years);
}

console.log(compound(1000, 0.05, 10));

Frequently Asked Questions

What is JSDoc and why is it useful?

JSDoc is a documentation standard for JavaScript using special comment syntax. IDEs use JSDoc annotations to provide autocomplete, type checking, and inline documentation. Tools like TypeDoc can generate HTML documentation from JSDoc comments.

Related Functions

Basic-SyntaxVariablesFunction-Declaration