JavaScript comments can be used to describe and explain JavaScript code, and to make the code it more readable and easy understandable.
Comments in JavaScript
Comment is nothing but it is a statement, Comments are not executed unlike the code on browser window.The javascript interpreter ignores the comments and continues execution till the end of file, when it encounters any comments.
Comment is also used to add information about the code, warnings or suggestions so that the end user or other developer can easily interpret the code.
There are 3-types of comments in JavaScript :
Single-Line Comment
Multi-Line Comment
ScriptDoc Comment
# Types of JavaScript Comments:
1.) Single-Line Comment : A single-line comment is represented by double forward slashes (//), It can be used before any statement.
➤ Example :
<script> // Write on browser document.write("Hello Javascript!"); // Write text an <h2> Heading document.write("<h2> Hello Javascript! </h2>"); </script>
Output :
Hello Javascript!
Hello Javascript!
2.) Multi-Line Comment : Multiline comments are used for large text descriptions of code.
A multi-line comment is represented by forward slash ( / )with asterisk (*) then asterisk with forward slash.
Multiple-line comments start with forward slash and asterisk (/*), and end with the reverse asterisk and forward slash (*/).
Note : It is also used for single-line comment.
➤ Example :
<script> /* First line: Write Simple write Second line: Write text an <h2> Heading */ document.write("Hello World!"); document.write("<h2> Hello World! </h2>"); </script>
Output :
Hello World!
Hello World!
3.) ScriptDoc Comment / JavaScript Documentation Comment :
ScriptDoc is JavaScript documentation technique, It is also known as JDC(JavaScript Documentation Comment).
It's good habit to write remarks, arguments or notes etc. on JavaScript.
➤ Syntax :
In JavaScript documentation style comment you can write following way :
<script> /** * ScriptDoc technique to write comment * @TagName Description * @author * @version * …. **/ </script>
Let's see the example of JavaScript Documentation Comment
➤ Example :
<script> /** * Multiplication of two numbers * @param {Number} a * @param {Number} b * @return {String} mult **/ 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 provides the many list of tags for documentation technique. There are few tags of ScriptDoc :
Tag | Description |
---|---|
@author Author-name [ email ] | Author of JavaScript file, functions, class. |
@classDescription Description | Brief description of the Class. |
@constructor | Specifies this function is a constructor. |
@example Example-code | Describe a real example for how to use this function. |
@id identifierName | Specifies ID for unique identification. |
@inherits inherit_class | Specifies inherited class. |
@memberOf Class | function, Property is member of specified class. |
@method | Specifies the method name in class. |
@exception { Exception } | Specifies exception type that throw this function. |
@namespace namespaceName | Specifies the external library file. |
@param { Type } ParameterName Parameter-Description | Specifies parameter of this function. |
@private | Indicate that a class or function is private. |
@projectDescription Description | Specifies on top of the page that describe the description of this file. |
@property {Type} | Indicate that specified property are instance of the class. |
@return {Type} Returns-Description | Specifies the return values of a function. |
@type {Type} | Specify the data type of this property. |
@version Version-Number | Specify the version number of file. |
Comments