The basis of any script is the code written by the developer. During the compilation of project code, it will be read and converted into a web page, application or its part. However, some scripts can contain large amounts of code, requiring a lot of time to read.
Also scripts can be created by a group of developers, which may require a lot of time to explain the code to all participants. Therefore, in many programming languages, including JavaScript, it's possible to leave comments directly in the script.
Comments are text inserts in scripts, highlighted with special syntax that is not a functional part of the code. This means that lines marked as comments do not affect script execution.
In programming, they are most often used to explain algorithms or specific conditions in the code. Also, sometimes developers use them to temporarily disable some lines of the code without removing it from the script.
Syntax
In JavaScript, comments are conventionally divided into two types: single-line and multi-line. They are identical in functionality, and you can use them as you wish. Yet they have different syntax, which determines their use.
Single-line comments are specified by //
characters, before the text that must be marked as a comment. Since they only hide the line they are written in, it is recommended to put them at the beginning of the line.
At the same time, there are no restrictions on their placement inside or after the code, unless it makes code unavailable for compilation. An example of a one-line comment:
let x = 1;
let z = 2;
//We create y variable to get sum of x and z
let y = x + z;
console.log(y);
Multi-line comments are a little trickier as there are two possible combinations. In front of the data that is to be defined as a multi-line comment, one must put /*
, and if after the data, */
respectively.
The entire area between these two designations will become a comment, regardless of what code is there. At the same time, comments have no length restrictions and can be used both for single lines and for the entire script.
/*Step 1. We create x and z variables
Step 2. We create y variable to get sum of x and z*/
let x = 1;
let z = 2;
let y = x + 4;
console.log(y);
If in some part of the document both types of comments are placed, comments located earlier in the script will take precedence. Correspondingly, a single-page comment can eliminate a multi-line comment area, if it is located before it.
Also, multi-line comments can contain lines marked as single-line comments. Yet, in some situations, for example, when we comment on a part of the line, we must also take into account the syntax of the surrounding elements:
Incorrect:
let firstVar = //a+4;
let //firstVar = a+4;
let firstVar = z+//4;
let/* firstVar = */z+4;
let firstVar = z+/*4*/;
Correct:
let firstVar = /*z+*/ 4;
let firstVar = z; /*+4*/
let firstVar = /*z+*/ 4;
/*let firstVar = z+4;*/
//let firstVar = z+4;
Comments can be applied to any part of the code, but there is one limitation. This concerns the fact that comments should not break the structure of the code. Accordingly, after comments there should be no unused brackets, inaccessible variables and everything else left, that could lead to a script crash.
It is also not recommended to use comments for chatting among developers. Industry standards recommend using comments only in cases where it is not possible to create clear-cut code with apprehensive function and variable names.
Test yourself
Task 1. The script performs arithmetic operations with the
firstVar
andsecondVar
variables, and outputs their result to the console. Modify the script with a single-line comment to omit subtraction.
let firstVar = 15;
let secondVar = 13;
console.log(firstVar + secondVar);
console.log(firstVar - secondVar);
console.log(firstVar * secondVar);
Solution. Comment the following on the fourth line:
// console.log(firstVar - secondVar);
Result
28
195
Task 2. In the following code sample, add a multi-line comment associated with the autumn and spring months
else...if
blocks.
let month, errMsg, season;
if (month === "December") {
season = "Winter";
} else if (month === "March") {
season = "Spring";
} else if (month === "April") {
season = "Spring";
} else if (month === "June") {
season = "Summer";
} else if (month === "August") {
season = "Summer";
} else if (month === "November") {
season = "Autumn";
} else {
errMsg = "Enter a correct month name";
}
Solution:
let month, errMsg, season;
if (month === "December") {
season = "Winter";
} /*else if (month === "March") {
season = "Spring";
} else if (month === "April") {
season = "Spring";
} */ else if (month === "June") {
season = "Summer";
} else if (month === "August") {
season = "Summer";
} /* else if (month === "November") {
season = "Autumn";
}*/ else {
errMsg = "Enter a correct month name";
}
Result
The code block for November and 2 blocks for March and April should be commented on. General functionality of the script will be preserved.