Home > Article > Web Front-end > Let’s talk about several comment characters in javascript
JavaScript is a widely used programming language. Annotations are a very important feature that can help developers enhance the readability, maintainability and reusability of code. A comment is a special statement that will not be executed, but it can tell other people some additional information about the code. There are a variety of comment characters available in JavaScript. This article will introduce these comments and their applicable scenarios.
Single-line comments use the "//" character to comment a line of code at the end of the line of code. For example:
var num1 = 10; var num2 = 20; //这里是注释,说明这个变量用途
A single line comment can also comment multiple statements in one line of code. For example:
var a = 1; //变量a var b = 2; //变量b var c = a + b; //计算a+b console.log(c); //输出c的值
Single-line comments are suitable for short comments on the code, such as simple explanations of the purpose of variables and functions of functions.
Multi-line comments use / / to wrap multiple lines of code, Comments must not affect the code. For example:
/* 这里是多行注释 可以在其中写入多行 代码 */ var num1 = 10; var num2 = 20;
Multi-line comments are suitable for commenting on a long piece of code, such as the implementation of a function, the function of a code segment, etc.
Documentation comments are a special type of multi-line comments. They start with "/*" and end with "/". Documentation comments can contain description information of functions, classes or objects, including parameters, return values and other information. For example:
/** * 计算两个数之和 * @param {number} x 第一个数 * @param {number} y 第二个数 * @returns {number} 返回两个数之和 */ function sum(x, y) { return x + y; }
Documentation comments are suitable for writing API documentation to make it easier for others to read and use the code.
In addition to the above three comments, there are some other comment methods:
When you write JavaScript When the code is open source, you should add some comments at the beginning of the file, such as "@license", to declare the license used by the code. For example:
/* * @license * My Project v1.0.0 * (c) 2022 My Name <me@example.com> * License: MIT */
When you write a function in your code, and the function has parameters, you can use the "@param" annotation to explain The type and role of each parameter. For example:
/** * 计算两个数之和 * @param {number} x 第一个数 * @param {number} y 第二个数 * @returns {number} 返回两个数之和 */ function sum(x, y) { return x + y; }
If the function you write returns a value, you can use the "@returns" annotation to identify the return value type of the function. For example:
/** * 计算两个数之和 * @param {number} x 第一个数 * @param {number} y 第二个数 * @returns {number} 返回两个数之和 */ function sum(x, y) { return x + y; }
When writing comments, pay attention to keeping the comments consistent with the code. Mastering the applicable comment methods can greatly improve the readability and maintainability of the code. At the same time, you should try to consider other people's reading and usage when designing the code. Writing standardized comments is also a good coding habit.
The above is the detailed content of Let’s talk about several comment characters in javascript. For more information, please follow other related articles on the PHP Chinese website!