Skip to content

Javascript Comments

Comments in JavaScript are annotations in the code that are ignored by the JavaScript engine during execution. They are used to explain the code, make it more readable, and prevent certain code blocks from executing. There are two types of comments in JavaScript: single-line comments and multi-line comments.

1. Single-line Comments

Single-line comments are used to comment a single line or part of a line. They start with //.

Examples:

  • Single-line comment on its own line:

// This is a single-line comment let x = 5;

  • Single-line comment at the end of a line:

let y = 10; // This is an inline comment

2. Multi-line Comments

Multi-line comments are used to comment multiple lines. They start with /* and end with */.

Examples:

  • Multi-line comment over multiple lines:

/* This is a multi-line comment. It can span multiple lines. */ let z = 15;

  • Multi-line comment on a single line:

/* This is a multi-line comment on a single line */ let w = 20;

Best Practices for Using Comments

  1. Keep comments clear and concise: Comments should be easy to read and understand.

// Calculate the sum of two numbers let sum = a + b;

  1. Avoid obvious comments: Do not write comments that are redundant or explain what is already clear from the code.

// This is redundant and should be avoided let x = 10; // Declare a variable x and assign it the value 10

  1. Use comments to explain why, not what: Focus on explaining the purpose and reasoning behind the code, rather than the code itself.

// Using the map function to double each element in the array let doubled = array.map(x => x * 2);

  1. Comment on complex logic: Provide comments on complex or non-obvious parts of the code to aid in understanding.

// Using a recursive function to calculate the factorial of a number

function factorial(n) {

    if (n === 0) {

        return 1;

    } else {

        return n * factorial(n – 1);

    }

}

  1. Update comments with code changes: Ensure comments are updated when the associated code is modified to prevent discrepancies.

// This comment needs to be updated if the logic changes

// Original: Function to add two numbers

function add(a, b) {

    return a + b;  // Now also logs the result

    console.log(a + b);

}

Example: Combining Single-line and Multi-line Comments

// This function calculates the sum of an array of numbers

function calculateSum(numbers) {

    /* Initialize sum to 0

       This will store the total sum of the numbers in the array */

    let sum = 0;

    // Loop through each number in the array

    for (let i = 0; i < numbers.length; i++) {

        // Add the current number to the sum

        sum += numbers[i];

    }

    // Return the calculated sum

    return sum;

}

/*

Test the function with an example array

Expected output: 10

*/

let result = calculateSum([1, 2, 3, 4]);

console.log(result);  // Output: 10

In this example, both single-line and multi-line comments are used to explain different parts of the code. The comments provide context and explanations without being redundant, making the code easier to understand and maintain.