JavaScript Comments
What is Comments in JavaScript ?
JavaScript comments are lines of text in a JavaScript file that are ignored by the interpreter.
They are used to add notes and explanations to the code, making it more readable and understandable for other developers.
There are two types of comments in JavaScript :
1) Single Line Comments
2) Multi-Line Comments
Single Line Comments begin with double slash "//" and continue until the end of the line, while multi-line comments start with single slash and one astric "/*' and end with one astric and single slash "*/".
Single Line Comment :
// This a Single Line Comment
Multi-Line Comment :
/* This an an Exmaple of
Multiline Comments in JavaScript */
Comments are an essential tool for developers, as they allow them to explain their thought process, provide context for the code, and convey any important information about the code, such as bugs or limitations.
It's a good practice to use comments in your JavaScript code, especially when working on a team or when you expect other developers to read and understand you code.
Example :
<h3>Hello Friends ! This Program Says, <span style="color:red" id="says"></span></h3>
<br>
<button onclick="say()">Click to Know !</button>
<script> // This a our Script tag, in that we have created say() Function
/* This is a simple code, it basically shows the output like,
A statement "Hello Friends ! This Program Says, " and a Button
which can calls a function which adds the content in Element which
has ID "syas".
Result is : Hello Friends ! This Program Says, Welcome To JavaScript */
function say() // Function which returns a value
{
document.getElementById("says").innerHTML = "Welcome To JavaScript !"
// This statement replaces the content of Element which has ID says
}
</script>
Prevent Execution of Code Using Comments :
We can also able to use the comments to Prevent the Execution of Specific Code.Suppose you have a code which can shows the First name, Middel Name, Last Name, and Full Name.
But you want to show only the FullName, then simply Comment the Specific code which can shows the First Name, Middel Name, and Last Name.
By using this method, you can easily able to prevent the execution of specific code without loosing the content.
After that when you want to show again the Commented Content, then simply remove the slashes ( / ).
<script>
// document.write("Hello ! This is the Example of JavaScript");
document.write("Hello ! This is an Example of Comments !")
</script>
Example :
<script>
var fname = prompt("Enter Your First Name !");
var mname = prompt("Enter Your Middel Name !");
var lname = prompt("Enter Your Last Name !")
var fullName = fname + " " + mname + " " + lname;
/* document.write("<br> Your Name : "+fname);
document.write("<br> Your Father Name : "+mname);
document.write("<br> Your Surname : "+lname); */
document.write("<br> Your Full Name : "+fullName);
</script>
Hope This Post will help you to learn JavaScript More...
0 Comments
Please do not add Any Spam link in Comments !