How to add comments to JavaScript code?

To add comments to JavaScript code, you can use two different types of comments: single-line comments and multi-line comments.

  1. Single-line comments: You can add a comment on a single line by using two forward slashes (//) at the beginning of the line. Anything after the // is considered as a comment.

    Example:

    // This is a single-line comment var age = 25; // This variable stores the age
  2. Multi-line comments: Multi-line comments allow you to add comments that span multiple lines. You can use /* to start a multi-line comment and */ to end it.

    Example:

    /* This is a multi-line comment. It can span multiple lines. */ var name = "John"; /* The following code block is commented out var age = 25; var address = "123 Main St"; */

It's important to note that comments are ignored by the browser and do not affect the execution of the code. They are used to provide explanations and make the code more readable to yourself and other developers.