- In programming, comments can also be used to prevent some code lines from being executed. This is useful when testing.
- There are single-line comments (which comment out one line or a part of one line) and multi-line comments (which comment out a block of code).
- Multi-line comments are more often used for formal documentations.
Comment vs. Comment Out
Check out the example of commenting out a single-line below:
//document.getElementById("test_el").innerHTML = "Some text"; document.getElementById("test_el2").innerHTML = "Some more text";
This example illustrates the commenting out a block of code:
/* document.getElementById("test_el").innerHTML = "some text"; document.getElementById("test_el2").innerHTML = "some more text"; */
The example below uses a single-line comment to explain a line of code below the comment:
In the example below, a comment is written on the same line as the code itself:
var x = 5; // Variable declaration var y = x + 99; // Different variable
For a single-line comment, you have to write two forward slashes:
//. Anything that goes after
/*. To end the comment, use these symbols
- Comments can be used to explain or hide the code so it is not executed.