- JS Introduction
- JS Introduction
- JS Comments
- JS Variables
- JS Datatypes
- JS Operators
- JS Type Conversions
- JS Control Flow
- JS Comparisons
- JS If else
- JS If else Ladder
- JS Ternary Operator
- JS Switch
- JS For Loop
- JS For In
- JS For Of
- JS While
- JS Do While
- JS Break & Continue
- JS Functions
- JS Function Declaration
- JS Function Parameters
- JS Return Statement
- JS Function Expressions
- JS Anonymous Functions
- JS Objects
- JS Objects
- JS Object Methods
- JS Object Constructors
- JS Object Destructuring
- JS Object Prototypes
- JS Map, Filter & Reduce
- JS ES6
- JS ES6
- JS let and const
- JS Arrow Functions
- JS Template Literals
- Destructuring Assignment
- JS Spread Operator
- JS Default Parameters
- JS Classes
- JS Inheritance
- JS Map
- JS Set
- JS Async
- JS Callbacks
- JS Asynchronous
- JS Promises
- JS Async/Await
- JS HTML DOM/BOM
- JS Document Object
- JS getElementbyId
- getElementsByClassName
- JS getElementsByName
- getElementsByTagName
- JS innerHTML
- JS outerHTML
- JS Window Object
- JS History Object
- JS Navigator Object
- JS Screen Object
JavaScript Comments
Think of comments as "sticky notes" for your source code. They are snippets of text that the JavaScript engine completely ignores when running your program. While they don't change how your app functions, they are vital for explaining why you wrote code a certain way, helping both your future self and your teammates understand the logic behind the script.
1. Single-Line Comments
Single-line comments are the most common type of comment in JavaScript. They begin with two forward slashes (//). Anything following these slashes on that same line will be treated as a comment and ignored by the browser.
// This is a standalone comment explaining the variable below
let userAge = 25;
let price = 99; // This is an "inline" comment at the end of a line
Single-line comments are perfect for quick notes or temporarily disabling a single line of code while you are testing different solutions.
2. Multi-Line Comments
Multi-line comments (also known as block comments) start with /* and end with */. They are ideal when you need to write a detailed explanation, document a function's parameters, or disable a large block of code during debugging.
/*
The following function handles the user authentication.
It checks the token validity and ensures the
session has not expired before granting access.
*/
function checkAccess() {
// Logic goes here
}
Best Practices for Using Comments
Be Clear and Concise:
- Write comments that provide immediate value. If a developer has to spend five minutes deciphering your comment, it’s not doing its job. Use plain, professional language.
Update Comments Regularly:
- Code evolves, and sometimes we forget to change the comments to match. An outdated comment is worse than no comment at all because it actively misleads other developers.
Avoid Redundant Comments:
- If your code is clean and your variable names are descriptive, you don't need to comment every line.
let total = 5; // sets total to 5This adds "noise" to your file without adding any new information.
Use Comments for Complex Logic:
- If you are using a complex regular expression, a specific mathematical formula, or a "hack" to fix a bug in a specific browser, document it! This prevents someone else from "fixing" your code and accidentally breaking it again.
Don't Comment Obvious Code:
- Strive for "self-documenting code." Instead of writing
let d = 86400; // seconds in a day, writeconst SECONDS_IN_A_DAY = 86400;. This makes the code readable without needing a comment.
Follow a Consistent Style:
- Whether you prefer single-line comments for everything or block comments for headers, stay consistent throughout your project to keep the codebase looking professional.
Example
Here is how a well-commented script might look in a real-world scenario, such as a shopping cart calculation:
// Calculate the final price after applying a seasonal discount
function getDiscountedTotal(price, discountPercentage) {
// Ensure we don't divide by zero or process negative prices
if (price <= 0) return 0;
let discountAmount = (price * discountPercentage) / 100;
return price - discountAmount;
}
/*
The following section initializes the checkout process.
We use a hardcoded tax rate of 7% as per regional policy.
*/
const TAX_RATE = 0.07;
let cartTotal = 150.00;
let finalPrice = getDiscountedTotal(cartTotal, 10); // Applying a 10% discount