Writing code that works is only the first step; writing code that is readable, maintainable, and understandable by your teammates (and future you) separates junior coders from seasoned software engineers.
JavaScript provides two primary syntax options for adding comments that are ignored by the JS engine:
Use for brief explanations of a single line:
1 // Calculate total cart price with GST tax rate of 18%
2 const taxRate = 0.18 ;
3 const totalWithTax = subtotal * ( 1 + taxRate ) ; // Inline comment
Use for longer architectural notes or temporary code disabling:
1 /*
2 The following authentication algorithm validates:
3 1. Token signature using HMAC-SHA256
4 2. Expiration timestamp (exp claim)
5 3. User active status in PostgreSQL database
6 */
7 function verifySessionToken ( token ) {
8 // ...
9 }
Top engineering teams use JSDoc comments. JSDoc allows code editors (like VS Code) to display rich tooltips, type hints, and autocompletion:
1 /**
2 * Calculates the final discounted price for an enrolled student.
3 *
4 * @param {number} originalPrice - The standard catalog price in INR.
5 * @param {number} discountPercent - Discount percentage (e.g., 20 for 20%).
6 * @returns {number} The final rounded price after discount.
7 */
8 function calculateCourseDiscount ( originalPrice , discountPercent ) {
9 const discountAmount = originalPrice * ( discountPercent / 100 ) ;
10 return Math . round ( originalPrice - discountAmount ) ;
11 }
When you hover your mouse over calculateCourseDiscount anywhere in your codebase, VS Code will display the parameters and return types automatically!
Pro Tip [!TIP]
Golden Rule of Clean Code : Good code is self-documenting. Comments should explain WHY something is done, not WHAT is done.
1 // Set age to 25
2 let age = 25 ;
3
4 // If user is greater than 18
5 if ( age > 18 ) {
6 // Allow entry
7 allowEntry ( ) ;
8 }
1 let d = 86400 ; // number of seconds in a day
✅ Clean & Self-Documenting: 1 const SECONDS_IN_A_DAY = 86400 ;
Follow standard JavaScript community conventions:
Identifier Type Convention Example Variables & Properties camelCaseuserName, totalAmount, isModalOpenFunctions & Methods camelCase (Verb-based)getUserProfile(), calculateTotal(), fetchCourses()Booleans Prefix with is, has, can isLoading, hasPermission, canSubmitClasses & Components PascalCaseUserCard, PaymentGateway, ShoppingCartGlobal Constants UPPER_SNAKE_CASEMAX_RETRIES, DEFAULT_TIMEOUT_MS
Strict Mode eliminates silent errors in JavaScript by converting them to throw runtime exceptions:
1 "use strict" ;
2
3 // Without strict mode, mistyping a variable creates an accidental global!
4 // With strict mode, this throws ReferenceError: userName is not defined
5 userName = "Sumit" ;
Note [!NOTE]
All modern ES6 modules (import/export) and React/Next.js components run in strict mode automatically.
Interactive Quiz • Knowledge Check
Practice Quiz What is the syntax for a single-line comment in JavaScript?
A <!-- Comment -->
B # Comment
C // Comment
D /* Comment
What is the primary purpose of writing JSDoc comments (/** ... */)?
A To compress JavaScript files
B To provide rich, structured documentation of function parameters, types, and return values that modern IDEs render as interactive tooltips
C To make code execute faster
D To prevent variable assignment
According to clean code principles, what should comments primarily explain?
A Every single line of code in detail
B Why a non-obvious business decision or complex algorithm was implemented, rather than stating what the syntax already shows
C The operating system version
D Personal opinions about coworkers
What casing convention is standard in JavaScript for functions and variable names?
A kebab-case
B snake_case
C camelCase (e.g. calculateTotalPrice)
D PascalCase
What does adding "use strict"; at the top of a JavaScript file enforce?
A It prevents using internet connections
B It enforces strict parsing and error handling, disallowing undeclared variables and silent errors
C It doubles RAM consumption
D It changes the language to TypeScript