MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Comments and Best Practices0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
2.1Variables: var, let, const2.2Data Types2.3Comments and Best Practices
HomeTutorialsJavaScript for BeginnersComments and Best Practices
Arithmetic Operators

Comments and Best Practices

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

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.


1. Comment Types in JavaScript

JavaScript provides two primary syntax options for adding comments that are ignored by the JS engine:

A. Single-Line Comments (//)

Use for brief explanations of a single line:

1// Calculate total cart price with GST tax rate of 18%
2const taxRate = 0.18;
3const totalWithTax = subtotal * (1 + taxRate); // Inline comment

B. Multi-Line Block Comments (/* ... */)

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*/
7function verifySessionToken(token) {
8 // ...
9}

2. Professional Documentation: JSDoc Comments (/** ... */)

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 */
8function 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!


3. When NOT to Write Comments (Code Smells)

Pro Tip
[!TIP] Golden Rule of Clean Code: Good code is self-documenting. Comments should explain WHY something is done, not WHAT is done.

❌ Bad: Redundant Comments

1// Set age to 25
2let age = 25;
3 
4// If user is greater than 18
5if (age > 18) {
6 // Allow entry
7 allowEntry();
8}

❌ Bad: Using Comments to Explain Cryptic Variable Names

1let d = 86400; // number of seconds in a day

✅ Clean & Self-Documenting:

1const SECONDS_IN_A_DAY = 86400;

4. Essential JavaScript Naming Conventions

Follow standard JavaScript community conventions:

Identifier TypeConventionExample
Variables & PropertiescamelCaseuserName, totalAmount, isModalOpen
Functions & MethodscamelCase (Verb-based)getUserProfile(), calculateTotal(), fetchCourses()
BooleansPrefix with is, has, canisLoading, hasPermission, canSubmit
Classes & ComponentsPascalCaseUserCard, PaymentGateway, ShoppingCart
Global ConstantsUPPER_SNAKE_CASEMAX_RETRIES, DEFAULT_TIMEOUT_MS

5. Enable Strict Mode: "use strict"

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
5userName = "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

Question 1
What is the syntax for a single-line comment in JavaScript?
Question 2
What is the primary purpose of writing JSDoc comments (/** ... */)?
Question 3
According to clean code principles, what should comments primarily explain?
Question 4
What casing convention is standard in JavaScript for functions and variable names?
Question 5
What does adding "use strict"; at the top of a JavaScript file enforce?
Next Lesson

Arithmetic Operators

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Data TypesArithmetic Operators

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext