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

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.