Single Line Comment
We already discussed comments during the very first video of this course. There the discussion was brief, but here you can find detailed information about mainly the single line comment.
When we want the compiler to ignore a line or number of lines in the program, we use the concept of the comment. By mentioning "single line comment," we mean that the code to be ignored (or a comment added to it) is single line and not multiple. However, single line commenting can be performed multiple times to make sure multiple lines are omitted by the compiler.
Syntax for single line comment
There isn't anything difficult to remember when it comes to the syntax of the single line comment. We just need to type // before any line or part of the line for the program to ignore it. The line is still in the program, but is not read by the compiler during the process of compilation. The general syntax is //trace("Yo mama wears army boots!");. For example, let us consider //trace("this line is commented out"); As soon as we placed // before trace, the whole line is neglected by the compiler and the text inside the double quotes is not printed.
Why does the single line comment even exist?
Many of you might wonder why we need to learn the theory of commenting, when its use is limited to commenting out a certain line when it's certainly much more simple to just delete it. Don't forget that deleting means that line is gone forever – play Taps for it. What if you need it later – especially if you're using it for testing purposes? You get to retype it, that's what. Now, if only you'd commented it out.... remove the // and you're back in business! That line is again part of your program. When commenting, nothing is deleted but the commented line is just ignored by the compiler and can be reused (or not) anytime you want!
Equally useful to having the compiler ignore a line, single line comments can also be used to describe the usage of a particular line within the program. Developers can write comments beside the actual code, such as:
trace("I am inside the newName function"); // this outputs only when the newName function is called
This is useful for developers maintaining or updating your code and learners because they can read the comments beside the line to understand its usage and why that line was included in the code.