JavaScript Comment
JavaScript comments are a valuable tool for conveying messages. They
provide additional information, warnings, or suggestions to help others
easily understand the code.
JavaScript comments are ignored by the JavaScript engine embedded in the
browser.
Advantages of JavaScript Comments
JavaScript comments offer two main advantages.
1. It helps
make the code easier to understand by providing explanations, allowing others to interpret the
code more easily.
2. To
prevent unnecessary code
execution, comments can be used to disable code
without deleting it. This is useful when you want to
temporarily disable certain actions in the code but may need to
enable them again later.
Types of JavaScript Comments
JavaScript has two types of comments.
1. Single line comment.
2. Multi line comment.
JavaScript Single line comment
It is indicated by two forward slashes (//).
It can be placed either before or after the statement.
Example 1
Let's look at an example of a single-line comment placed before the
statement.
<Script>
// Print a message to the console
console.log("Hello GoCourse");
</Script>
Let's look at an example of a single-line comment added after the
statement.
Example 2
Here is an example:
<Script>
var x = 5; // declare a variable x with value 5
console.log(x); // print the value of x to the console
</Script>
JavaScript multi line comment
It can be used for both single-line and multi-line comments, making it more
versatile.
It is represented by a forward slash followed by an asterisk and ending
with an asterisk followed by a forward slash. For example:
Example 1
/* This is a multi line comment*/
It can be placed before, after, or in the middle of a statement.
Example 2
Here are some examples:
<Script>
/* This is a comment */
alert("Hello GoCourse");
</Script>