Advertisement

Google Ad Slot: content-top

JS Comments

In JavaScript, comments are used to write notes, explanations, or temporarily disable parts of code. Comments are ignored by the JavaScript engine during execution, making them useful for improving code readability and documentation.


There are two types of comments in JavaScript:


1. Single-Line Comments


Single-line comments start with //. Everything after // on that line will be treated as a comment

Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comments</h2>
<p id="demo"></p>
<script>
let x = 5; // Declare x, give it the value of 5
let y = x + 2; // Declare y, give it the value of x + 2
// Write y to demo:
document.getElementById("demo").innerHTML = y;
</script>
</body>
</html>
Try it yourself

2. Multi line comments


Multi-line comments start with /* and end with */. They can span multiple lines and are useful for longer explanations or to comment out blocks of code.

Example
<!DOCTYPE html>
<html>
<body>

<h1 id="myH"></h1>
<p id="myP"></p>

<script>
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
*/
document.getElementById("myH").innerHTML = "JavaScript Comments";
document.getElementById("myP").innerHTML = "My first paragraph.";
</script>

</body>
</html>
Try it yourself