You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/basics/comments.md
+33-7Lines changed: 33 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,23 +8,49 @@ description: Comments are used to mark annotations for other programmers or smal
8
8
9
9
Comments are statements that will not be executed by the interpreter. They are used to mark annotations for other programmers or small descriptions of what code does, thus making it easier for others to understand what your code does. They are also used to temporarily disable code without affecting the program control flow.
10
10
11
-
In JavaScript, comments can be written in two different ways:
11
+
## Types of Comments
12
12
13
-
***Single-line comments**: It starts with two forward slashes (`//`) and continue until the end of the line. Anything following the slashes is ignored by the JavaScript interpreter. For example:
13
+
### Single Line Comments
14
+
15
+
Single-line comments start with two forward slashes (`//`) and continue until the end of the line. Anything following the slashes is ignored by the JavaScript interpreter.
14
16
15
17
```javascript
16
18
// This is a comment, it will be ignored by the interpreter
17
-
let a ="this is a variable defined in a statement";
19
+
let a ="This is a variable";
18
20
```
19
21
20
-
***Multi-line comments**: It starts with a forward slash and an asterisk (`/*`) and end with an asterisk and a forward slash (`*/`). Anything between the opening and closing markers is ignored by the JavaScript interpreter. For example:
22
+
You can also use them at the end of a line:
23
+
24
+
```javascript
25
+
let b =5; // This is also a valid single-line comment
26
+
```
27
+
28
+
### Multi-line Comments
29
+
Multi-line comments start with /* and end with */. Everything in between is ignored.
21
30
22
31
```javascript
23
32
/*
24
-
This is a multi-line comment,
25
-
it will be ignored by the interpreter
33
+
This is a multi-line comment.
34
+
You can use it to explain more complex logic
35
+
or block out code temporarily.
26
36
*/
27
-
let a ="this is a variable defined in a statement";
37
+
let x =10;
38
+
```
39
+
40
+
### Real-life Examples of Comments
41
+
```javascript
42
+
// Change heading:
43
+
document.getElementById("myH").innerHTML="My First Page";
44
+
45
+
// Change paragraph:
46
+
document.getElementById("myP").innerHTML="My first paragraph.";
47
+
```
48
+
49
+
They are also useful for debugging purposes:
50
+
51
+
```javascript
52
+
let total =0;
53
+
// total = calculateTotal(items); // Temporarily disabled during testing
28
54
```
29
55
30
56
Including comments in code is essential for maintaining code quality, enabling collaboration, and simplifying the debugging process. By providing context and explanations for various parts of the program, comments make it easier to understand the code in the future. Therefore, it is considered a beneficial practice to include comments in code.
0 commit comments