Comments are lines of code that JavaScript will intentionally ignore. Comments are a great way to leave notes to yourself and to other people who will later need to figure out what that code does.
in other words, JavaScript comments can also be used to prevent execution, when testing alternative code.
Advantages of JavaScript comments:-
- To make code easy to understand It can be used to elaborate the code so that end user can easily understand the code.
- To avoid the unnecessary code It can also be used to avoid the code being executed. Sometimes, we add the code to perform some action. But after sometime, there may be need to disable the code. In such case, it is better to use comments.
Two way to comment our code:-
1)Single line (Ex: - //)
2)Multi line (Ex: - /* */)
Single line Code:-
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript Comments
</title>
<script>
// Function to add two numbers
function add() {
// Declare three variables
var x, y, z;
// Input a number and store it into variable x
x = Number(document.getElementById("num1").value);
// Input a number and store it into variable x
y = Number(document.getElementById("num2").value);
// Sum of two numbers
z= x +y;
// Return the sum
document.getElementById("sum").value = z;
}
</script>
</head>
<body>
Enter the First number: <input id="num1"><br><br>
Enter the Second number: <input id="num2"><br><br>
<button onclick="add()">
Sum
</button>
<input id="sum">
</body>
</html>
Output:-
Before clicking on the button:

After clicking on the button:


After clicking on the button:

Multiline Code:-
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Comments</title>
<script>
/* Script to get two input from user
and add them */
function add() {
/* Declare three variable */
var x, y, z;
/* Input the two nos. num1, num2
Input num1 and store it in x
Input num2 and store it in y
The sum is stored in a variable z*/
x = Number(document.getElementById("num1").value);
y = Number(document.getElementById("num2").value);
z = x + y;
document.getElementById("sum").value = z;
}
</script>
</head>
<body>
Enter the First number : <input id="num1" /><br /><br />
Enter the Second number: <input id="num2" /><br /><br />
<button onclick="add()">Sum</button>
<input id="sum" />
</body>
</html>
Output:-
Before clicking on the button:

After clicking on the button:


After clicking on the button:

0 Comments