JavaScript Comments
Comments are notes you leave in your code for yourself and other developers. The computer ignores them completely, but humans rely on them to understand intent, logic, and context.
JavaScript supports three kinds of comments: single-line, multi-line, and JSDoc documentation comments.
Single-Line Comment
// This is a single-line comment
console.log("Hello");
Single-Line After Code
console.log("Hi"); // Inline comment
Multi-Line Comment
/*
This is a multi-line comment.
It spans multiple lines.
*/
console.log("World");
Commenting Out Code
// console.log("This won't run");
console.log("This will run");
Multi-Line Commenting Out
/*
console.log("Skipped line 1");
console.log("Skipped line 2");
*/
console.log("Active line");
JSDoc Comment
/**
* Adds two numbers together.
* @param {number} a - First number
* @param {number} b - Second number
* @returns {number} The sum
*/
function add(a, b) {
return a + b;
}
JSDoc with Object Params
/**
* @param {Object} user - User information
* @param {string} user.name - User's full name
* @param {number} user.age - User's age
*/
function showUser({ name, age }) {
console.log(name, age);
}
Comment-Driven Development
// TODO: Implement error handling
// FIXME: This doesn't handle edge cases
// HACK: Temporary workaround for API bug
console.log("Working on it...");