Does JavaScript have a standard for commenting functions?


Comments are used to explain the code flow and want to prevent the code from executing. In JavaScript, there are two types of comments. One is single-line comments, used to comment on the single-line code to prevent the code from executing or to explain the code. Another one is multi-line comments, used to comment on blocks of code to prevent the code from the execution or to explain code that is added in or code.

By adding comments, we will give clarity to other developers also why code is added and what that particular code will do. Commented code will be ignored by the JavaScript engine during execution.

In JavaScript, two types of comments are available – single-line and multi-line comments.

The single-line comments can be started with // and ended with the end of the line. Any start line starts with //, then it will be ignored by the engine.

// it’s a single-line comment

We present multi-line comments as forward slash (/) with an asterisk (*) and ends as an asterisk (*) and followed by forward slash (/).

/*
   multiple-line comment
   in JavaScript
*/

Now, we will check how we can use these single-line and multi-line comments for commenting functions.

Commenting the Functions

We could use the single-line and multi-line comments for commenting function in JavaScript. Now, we will check how we can comment functions in a standard way.

Example 1

In the below example, we apply single as well as multi-line comments for commenting the function multiply.

<!DOCTYPE html> <html> <body> <h1>Commenting functions in js</h1> <p id = "result"></p> <script> /* Script to multiply few numbers and giving result to html tag */ function multiply(a, b, c) { return (a*b*c) } let res = multiply(5,7,9); // assigning result to a p tag document.getElementById("result").innerHTML = res; </script> </body> </html>

Example 2

In the below example, we apply single as well as multi-line comments for commenting the function multiply.

<!DOCTYPE html> <html> <body> <h1>Commenting functions in JavaScript</h1> <p id = "result"></p> <script> /* Script to add two numbers and giving result to html tag */ function add(a, b) { return (a+b) } /* function add(a, b) { return a+b; } */ let res = add(20,50); // assigning result to the p tag document.getElementById("result").innerHTML = res; </script> </body> </html>

Here, we have seen single, and multi-line comments used to prevent the block/ line of code from execution and also to explain the code.

Example 3

Now, we can check another example for commenting functions in a standard way. Like,

<!DOCTYPE html> <html> <body> <h1>Commenting functions in js</h1> <p id = "result"></p> <script type="text/javascript"> /** * Multiply two numbers. * @param {number} value1, first number to multiply. * @param {number} value2, second number to multiply. * @return {number} The result of multiplying value1 and value2. */ function multiply(a,b) { return a*b; } var value1 = 10; var value2 = 20; var res = multiply(value1, value2); document.getElementById("result").innerHTML = res; </script> </body> </html>

Here, we explained the multiply

In this tutorial, we learned to use single-line and multi-line comments for commenting functions in JavaScript.

Updated on: 26-Aug-2022

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements