English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript basic tutorial

JavaScript object

JavaScript function

JS HTML DOM

JS browser BOM

AJAX basic tutorial

JavaScript reference manual

JavaScript Comments

If the comment is only a single line of text, the JavaScript interpreter completely ignores this line.

Comments are usually added to provide additional information related to the source code, such as program descriptions, parameter explanations, etc.

Comments can also be used to block or skip execution when testing code.

Single-line comments

Single-line comments start with double forward slashes (//Start with a forward slash followed by the comment text.

// Print "Hello World" to the document
document.write("Hello World");
Test and see‹/›

When writing comments, please indent them at the same level as the code directly below them:

// Initialize the function
function showYear() {
   // date object
   let obj = new Date();
   // Store the current year in the variable "myYear"
   let myYear = obj.getFullYear();
   // Use the following command to print the current year to the HTML element with ID="para"
   document.getElementById("para").innerHTML = myYear;
}
Test and see‹/›

When a single-line comment appears at the end of a code line, it is calledInline comments.

let x = 20;// Assign a number to y
let y = x + 5; //  x + 5 Assign to y
document.write(y); // Print the value of variable y to the document
Test and see‹/›

Multiline comments

Multiline comments or block comments are long-form comments used to introduce and explain a section of code. Typically, these types of comments are placed at the top of a file or before particularly complex code blocks.

Multiline comments start with a forward slash and an asterisk (/*Start with an asterisk and a forward slash (*/End.

/* Initialize the greetUser function
Assign the username to a variable and print it out
greeting statement. */
function greetUser() {
  let name = prompt("What is your name?");
  document.getElementById("output").innerHTML = "Hello " + name;
}
Test and see‹/›

You may also see a slightly modified version of the block comment syntax, which is/**Start with the beginning of the comment block on the left and with an asterisk.

/**
  *Initialize the greetUser function.
  *Assign the username to a variable
  *and print the greeting.
  */
function greetUser() {
  let name = prompt("What is your name?");
  document.getElementById("output").innerHTML = "Hello " + name;
}
Test and see‹/›

Block comments are usually used in formal documents.

Use comments to prevent execution

Comments can also be used to quickly and easily prevent code execution for testing and debugging purposes.

// addTwoNumbers(5, 5);
multiplyTwoNumbers(6, 3);
Test and see‹/›

Single-line comments and multi-line comments can be used to comment out code, depending on the size of the part to be switched.

/*
document.write("Hello World<br>");
document.write("Hello World<br>");
document.write("Hello World<br>");
document.write("Hello World");
*/
Test and see‹/›

Comments in code can help you determine the location of errors or evaluate code lines that provide the most practical utility when determining the logic of the program.