Write JavaScript comment to make the code more readable and easy understandable. JavaScript mixed with JavaScript Comment follow the good coding style. Also every script writer must have this skill to follow good coding standard.
You can write JavaScript comment,
- Single-line Comment
- Multiline comment
- ScriptDoc Style Comment
JavaScript Single-line Comment
A single-line JavaScript comment beginning with double forward slash (//). Following is an example of Single line comment.
Syntax
<script type="text/javascript">
// JavaScript single line comment write on here....
</script>
Example
// Write on browser
document.write("Hello World!");
// Write text an <h2> Heading
document.write("<h2> Hello World! </h2>");
document.write("Hello World!"); // Write Comments at end of a Line
// Execute JS Code that are write in comment line
//document.write("<h2>This text is h2 Heading</h2>");
JavaScript Multi-line comment
A multi-line JavaScript comment beginning with forward slash and asterisk (/*), and end with the reverse asterisk and forward slash (*/).
Syntax
<script type="text/javascript">
/*
Multiline comment write on here....
...
*/
</script>
Example
/*
JS to write on body section
1st: Simple write
2nd: write text an <h2> Heading
*/
document.write("Hello World!");
document.write("<h2> Hello World! </h2>");
JavaScript Documentation Comment (ScriptDoc Style Comment)
A documentation style JavaScript comment you can write following way. It's good habit to write remarks, arguments, notes etc on JavaScript.
Syntax
<script type="text/javascript">
/**
* ScriptDoc technique to write comment
* @TagName Description
* ….
**/
</script>
ScriptDoc is JavaScript documentation technique. ScriptDoc provides the list of tags. Following are few tags,
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. |
@deprecated | Specifies this function is deprecated, no longer use. |
@example Example-code | Describe a real example for how to use this function. |
@exception { Exception } | Specifies exception type that throw 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. |
@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. |
Example
/**
* Addition of two numbers
* @param {Number} a
* @param {Number} b
* @return {String} Sum
**/
function addition(a, b) {
sum = a + b;
return "Result is " + sum.toString();
}
result = addition(5, 10);
document.write(result);