Javascript 简明教程

JavaScript - Comments

JavaScript Comments

JavaScript 注释用于解释代码的目的。 comments 不会作为程序的一部分执行,它们仅仅是为了让人类开发人员更好地理解代码。

JavaScript comments are used to explain the purpose of the code. The comments are not executed as a part of program and these are solely meant for human developers to understand the code better.

你可以将注释用于多种用途,诸如:

You can use comments for various purposes, such as −

  1. Explaining the purpose of a particular piece of code.

  2. Adding documentation to your code for yourself and others who may read it.

  3. Temporarily disabling or "commenting out" a block of code for testing or debugging without deleting it.

一个好的开发人员总是会写注释来解释代码。

A good developer always writes a comment to explain the code.

用 JavaScript 编写注释的常见方法有两种:

There are two common ways to write comments in JavaScript −

  1. Single-line comments

  2. Multi-line comments

Single-line comments in JavaScript

我们可以使用 double forward slash (//)开始单行注释。我们可以将注释写在 // 和行尾之间。

We can start the single-line comment with the double forward slash (//). We can write comment between // and end of the line.

Syntax

单行注释的语法如下:

The syntax for the single-line comments is given below −

<script>
   // single-line comment message
</script>

Example

在下边的示例中,我们使用单行注释对 JavaScript 代码的每一步进行了解释。在输出中,代码打印连接的字符串。

In the example below, we have explained each step of JavaScript code using single-line comments. In the output, the code prints the concatenated string.

<html>
<head>
  <title> Single line comments </title>
</head>
<body>
  <script>
    // Defining the string1
    var string1 = "JavaScript";
    // Defining the string2
    var string2 = "TypeScript";
    // Printing the strings
    document.write(string1, " ", string2);
  </script>
</body>
</html>

Example

在下边的示例中,我们注释掉“bool1=false;”这一行以避免执行此代码。在输出中,我们可以看到 bool1 变量的值为 true,因为我们注释掉了更新 bool1 变量值的代码。

In the example below, we have commented the 'bool1 = false;' line to avoid execution of that code. In the output, we can see that value of the bool1 variable is true, as we commented out the code, which updates the bool1 variable’s value.

另外,我们也可以在编写代码之后添加单行代码,就像我们在 document.write() 方法之后添加的那样。

Also, we can add the single line code after writing the code, as we added after the document.write() method.

<html>
<head>
  <title> Single line comments </title>
</head>
<body>
  <script>
    var bool1 = true;
    // bool1 = false;
    document.write("The value of the bool1 is: ", bool1); // print variable value
  </script>
</body>
</html>

Multi-line comments in JavaScript

当我们需要注释多行代码或解释较长的代码时,多行注释就很有用了。我们可以将多行注释写在 //. 之间。

The multi-line comment is useful when we require to comment the multiple lines of code or explain the larger codes. We can write multi-line comments between the / and /.

Syntax

多行注释的语法如下:

The syntax for the multi-line comments is given below −

<script>
   /* First line of comment message
   The second line of the comment message */
</script>

Example

在下边的示例中,我们定义了变量“a”和“b”。之后,我们在 3 行代码中编写了交换“a”和“b”的代码,并使用多行注释将这些代码注释掉了。

In the example below, we have defined 'a' and 'b' variables. After that, we have written the code to swap values of 'a' and 'b' in 3 lines and comment out using the multi-line comments.

输出将打印“a”和“b”的实际值,因为浏览器会忽略注释的代码。

The output prints the actual values of the 'a' and 'b' as the browser ignores the commented code.

<html>
<head>
  <title> Multi line comments </title>
</head>
<body>
  <script>
    var a = 100;
    var b = 200;

    /* a = a + b;
    b = a - b;
    a = a - b; */

    document.write("a = " + a + "<br>" + "b = " + b);
  </script>
</body>
</html>

始终在代码中使用注释,因为这有助于其他协作者理解你的代码。

Always write comments in your code as it helps other collaborators to understand your code.