Comments
Comments are lines in your code that the JavaScript engine completely ignores. They exist purely for humans — to explain logic, document intent, and make complex code understandable months later.
The Two Types of Comments
JavaScript supports two comment syntaxes. Both are completely ignored by the engine — they produce zero output and have no effect on how the code runs.
Single-Line Comments — //
Single-line comments start with // and run
to the end of the line. They can appear on their own line or after code on the same line (inline
comments).
// This whole line is a comment — fully ignored by JS
let age = 25; // inline comment — code runs, comment is ignored
// Variables for storing user data
let firstName = "Alice";
let lastName = "Smith";
// Calculate birth year (approximate)
let currentYear = 2025;
let birthYear = currentYear - age; // 2000
// Commenting out code during debugging:
// console.log("This line won't run");
console.log("This one runs");
Multi-Line Block Comments — /* */
Block comments start with /* and end with
*/. Everything in between — including newlines — is treated as a
comment.
/*
This is a multi-line comment.
It can span as many lines as you need.
Great for detailed explanations.
*/
function calculateTax(income, rate) {
/*
Tax formula:
1. Multiply income by the rate (as a decimal)
2. Round to 2 decimal places
Note: rate is passed as percentage (e.g. 20 for 20%)
*/
return Math.round((income * rate / 100) * 100) / 100;
}
/* Can also be used inline — not recommended, but valid */
let x = 5 + /* this 3 is ignored */ 3;
console.log(x); // 8
Commenting Out Code
One of the most practical uses of comments during development is temporarily disabling code without deleting it. This is called "commenting out".
function processOrder(order) {
console.log("Processing:", order);
// Temporarily disabled while testing new logic below:
// sendEmailConfirmation(order.email);
// updateDatabase(order);
// New logic being tested:
queueForProcessing(order);
}
/*
Entire section disabled during refactoring:
function oldPaymentFlow(amount) {
const result = legacyPaymentAPI(amount);
return result.status === 200;
}
*/
JSDoc — Documentation Comments
JSDoc is a standard format for documenting JavaScript functions, parameters, and return values using specially formatted block comments. Editors like VS Code read JSDoc and show it as intelligent tooltips when you hover over function calls.
* Calculates the total price including tax.
*
* @param {number} price - The base price before tax
* @param {number} taxRate - Tax rate as a percentage (e.g. 18)
* @returns {number} Total price rounded to 2dp
*/
function getTotalPrice(price, taxRate) {
return Math.round(price * (1 + taxRate / 100) * 100) / 100;
}
/**
* Greets a user by name.
*
* @param {string} name - The user's first name
* @returns {string} - The greeting message
*/
function greetUser(name) {
return `Hello, ${name}! Welcome to K2infocom. 🎉`;
}
/**
* Finds a user by their ID.
*
* @param {number} id - The user's unique ID
* @param {boolean} [active] - Optional: only search active users
* @returns {Object|null} - The user object, or null if not found
*/
function findUser(id, active = true) {
// implementation here...
}
Comment Best Practices
Not all comments are created equal. Good comments explain the why, not the what. The code already shows what is happening — the comment should explain the intent, context, or any non-obvious decision.
const MAX_RETRIES = 3;
// Using Math.floor not parseInt — safer for
// non-integer inputs (parseInt("3abc") = 3)
const index = Math.floor(value);
// TODO: replace with pagination once API supports it
const all = fetchAll();
let x = 5;
// Add a and b
let sum = a + b;
// Loop through users array
users.forEach(u => process(u));
TODO, FIXME & HACK Markers
Convention markers help teams track pending work in code. VS Code and most IDEs
highlight these automatically, and tools like todo-tree list
them all in a sidebar.
// TODO: add input validation before deploying
function saveUser(data) {
database.insert(data);
}
// FIXME: this breaks when timezone offset is negative
function formatDate(date) {
return date.toISOString().split('T')[0];
}
// HACK: workaround for Safari bug — remove after iOS 18
if (isSafari()) {
element.style.transform = 'translateZ(0)';
}
// NOTE: this function is intentionally synchronous
function readConfig() { /* ... */ }
Try It Yourself
Practice adding comments to the code below. Try commenting out one of the lines to see it stop running.
Which comment syntax is used to write multi-line documentation (JSDoc)?
//creates a single-line comment — everything after it on that line is ignored./* */creates a multi-line block comment — spans as many lines as needed.- Comments are completely ignored by the JS engine — zero performance cost.
- Use /** */ (JSDoc) to document functions — editors show it as hover tooltips.
- Good comments explain the why, not the what. Avoid restating what the code already shows.
- Use TODO / FIXME / HACK / NOTE markers to flag pending work and known issues.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.
//on that line is ignored. Most commonly used for short explanations and inline notes.multiple lines */
/*and*/is ignored, regardless of line breaks. Used for longer explanations and JSDoc.