Learn JavaScript Comments





Part 1


Javascript comments are not part of the code, they are used to describe our code to other developers, or Make it easy for us to understand in the future


For example, If you have to change or modify code lines of website you made 2 years before, you would probably benefit alot from comments


Another use of comments is to simply "mark" line of code so the computer will not process it, and when we need that code, we can just unmark it as we will learn in this lesson.



Single Line Comments


Single line comments start with //


The computer will ignore any text after the opening //


The next example will demonstrate single line comment:



Code Example:

                   
                    
                     // Change number:
document.getElementById("10").innerHTML = "50";

   
 // Change paragraph:
document.getElementById("test").innerHTML = "leard to code.";
                 
                 
Try yourself >>



You can also place the comments at the same line (after the code at the end.)


For example: "code" // comment.







Multi-line Comments



Part 2


If you want to write multi-line comment, instead of // we will write /*


Notice that every code after the opening sign /* will be marked as comment, so once you ready to add code, we will write closing sign */


The next example will demonstrate multi line comment:


Code Example:

                      
                   /*
Multi-line comment:
The next code line will
replace the number 10 with 50
*/
document.getElementById("10").innerHTML = "50";
                 
                 
Try yourself >>



The next example will demonstrate situation where we use multi-line comments to prevent execution of code (Simply remove the opening and closing marks, if you wish to let Javascript run the code.)


Code Example:

                      
                   /*
document.getElementById("test-1").innerHTML = "10";
document.getElementById("test-2").innerHTML = "20";
document.getElementById("test-3").innerHTML = "30";
document.getElementById("test-4").innerHTML = "40";
*/
                 
                 










Learn Javascript easily with FreeCodeWiki, our main goal is to allow students to learn to code, we offer online coding courses for free.

Navigation