Ankaj Gupta
January 11, 2019

How To Write Comments in JavaScript

Comments in JavaScript

JavaScript comments describe and explain code and improve readability. A comment is a statement that is not executed. The JavaScript interpreter skips comments, so they don't affect runtime.

Why Use Comments?

Comments add details, warnings, and suggestions so users and other developers can understand the code.

There are 3 types of comments in JavaScript:

  • Single-Line Comment
  • Multi-Line Comment
  • ScriptDoc Comment

1. Single-Line Comment

Single-line comments use double forward slashes // and can appear before any statement.

<script>
    // Write on browser
    document.write("Hello Javascript!");
    
    // Write text in <h2> Heading
    document.write("<h2> Hello Javascript! </h2>");  
</script>

Output:

Hello Javascript!

Hello Javascript!

2. Multi-Line Comment

Multi-line comments are used for longer descriptions. They start with /* and end with */.

Note: Multi-line comments can also be used for single-line comments if you prefer.

<script>
    /*
        First line: Write Simple write
        Second line: Write text in <h2> Heading
    */
    document.write("Hello World!");
    document.write("<h2> Hello World! </h2>");  
</script>

Output:

Hello World!

Hello World!

3. ScriptDoc Comment / JavaScript Documentation

ScriptDoc (JSDoc) is a JavaScript documentation technique for documenting functions, parameters, return values, and more.

Best Practice: Use JSDoc comments to document functions, arguments, and return types for better code maintainability.

Syntax:

<script>
    /** 
     * ScriptDoc technique to write comment
     * @TagName Description
     * @author   
     * @version 
     * ….
    **/ 
</script>

Complete Example:

<script>
    /** 
     * Multiplication of two numbers 
     * @param {Number} a - First number
     * @param {Number} b - Second number
     * @return {String} mult - Result of multiplication
    **/ 
    function multiplication(a, b) { 
        mult = a * b;
        return "Output is : " + mult.toString(); 
    }
    
    result = multiplication(7, 10);
    document.write(result);
</script>

Output:

Output is : 70

ScriptDoc Tags Reference

Common JSDoc tags for documenting JavaScript:

Tag Description
@author Author of JavaScript file, functions, class
@classDescription Brief description of the Class
@constructor Specifies this function is a constructor
@example Describe a real example for how to use this function
@method Specifies the method name in class
@param Specifies parameter of this function
@private Indicates that a class or function is private
@property Indicates specified property are instance of the class
@return Specifies the return values of a function
@type Specify the data type of this property
@version Specify the version number of file

Comment Types Comparison

Comment Type Syntax Best For
Single-Line // comment Quick notes, inline explanations
Multi-Line /* comment */ Longer explanations, disabling code
JSDoc /** */ Function documentation, API generation

Best Practices

✅ Do's

  • • Document complex logic
  • • Explain "why", not just "what"
  • • Use JSDoc for functions
  • • Keep comments up-to-date

❌ Don'ts

  • • Don't state the obvious
  • • Avoid outdated comments
  • • Don't over-comment simple code
  • • Remove commented-out code

Summary

JavaScript comments improve code readability. Use single-line comments (//) for short notes, multi-line comments (/* */) for longer descriptions, and JSDoc comments (/** */) for function documentation.

Good commenting practices improve maintainability and help other developers understand your code.

JavaScript web development and designing

Join the discussion

Comments

0 comments

No comments yet — be the first to share your thoughts.

Related Posts