First the straightforward part: how do we place comments in our code?
Single line comments
// Change heading:
document.getElementById("myH").innerHTML = "My First Page";
Single line comments at end of the line:
const x = 5; // Declare x, give it the value of 5
Coding well in JavaScript: JSDoc
W3Schools Comments
<!-- Write
your comments here -->
<!-- Write your comments here -->
/* Comment */
/*
A comment
which stretches
over several
lines
*/
Now for the hard part: when to comment? When you work for different companies, you will see different styles. Embrace something you like, and then learn to let go. Google on "when to comment code?" and you'll find a big bunch of different opinions.
The general concept is, however, that it is there to help make the code more easy to understand. Note, however, that comments can also make code more difficult to understand when not applied properly.