Home > Web Front-end > JS Tutorial > How to add comments in JavaScript

How to add comments in JavaScript

藏色散人
Release: 2023-01-05 16:14:21
Original
6356 people have browsed it

How to add comments in JavaScript: 1. Use the "//" symbol to make a single-line comment. Any text between "//" and the end of the line will be ignored by JavaScript; 2. Use the "/*" symbol to comment. ” and end with “*/” to implement multi-line comments.

How to add comments in JavaScript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript Comments

JavaScript comments are used to explain JavaScript code and enhance its readability.

JavaScript comments can also be used to prevent execution when testing alternative code.

Single-line comments

Single-line comments start with //.

Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

This example uses a single-line comment before each line of code:

Example

// 改变标题:
 document.getElementById("myH").innerHTML = "我的第一张页面";
// 改变段落:
 document.getElementById("myP").innerHTML = "我的第一个段落。";
Copy after login

This example uses a single-line comment at the end of each line to explain the code:

Example

var x = 5;      // 声明 x,为其赋值 5
var y = x + 2;  // 声明 y,为其赋值 x + 2
Copy after login

Multi-line comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored by JavaScript.

This example uses multi-line comments (comment blocks) to explain the code:

Example

/*
 下面的代码会改变
 网页中
 id = "myH" 的标题
 以及 id = "myP" 的段落:
*/
document.getElementById("myH").innerHTML = "我的第一张页面";
document.getElementById("myP").innerHTML = "我的第一个段落。";
Copy after login

Comments: It is most common to use single-line comments.

Tip: Comment blocks are often used for official statements.

Use comments to prevent execution

Using comments to prevent code execution is great for code testing.

Adding // before a line of code will change the executable line of code to a comment.

This example uses // to prevent the execution of lines of code:

Example

//document.getElementById("myH").innerHTML = "我的第一张页面";
document.getElementById("myP").innerHTML = "我的第一个段落。";
Copy after login

This example uses comment blocks to prevent the execution of multiple lines of code:

Example

/*
document.getElementById("myH").innerHTML = "我的第一张页面";
document.getElementById("myP").innerHTML = "我的第一个段落。";
*/
Copy after login

Recommended study: "javascript advanced tutorial"

The above is the detailed content of How to add comments in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template