Welcome 🎉

logo

ReactLMS

Search
Light Mode
Contact Us

2 min to read

Contact us

No results for your search.
Sorry, an unexpected error occurred

Concept


Comments are an important part of writing and maintaining source code. They allow programmers to add information, explain the source code, and create notes to make the code more understandable.

JavaScript supports two types of comments:


Single-line comments


Commenting a line in JavaScript starts with two slashes // and extends to the end of the line. All content after the // will be considered as a comment and JavaScript will ignore it when executing the source code.

// Đây là một nhận xét một dòng
var x = 5; // Gán giá trị 5 cho biến x






In the example above, a single-line comment is used to explain the source code and make a note about assigning the value 5 to the variable x.


Multi-line comments


Comments in JavaScript that span multiple lines start with /* and end with */. Everything between these two symbols is considered a comment and JavaScript will ignore it when executing the source code.

/*
Đây là một ví dụ về nhận xét nhiều dòng.
Trong ví dụ này, chúng ta giải thích cách sử dụng
nhận xét nhiều dòng để ghi chú dài và chi tiết.
*/
var y = 10; // Gán giá trị 10 cho biến y






In the example above, multi-line comments are used to explain the source code and make a note about assigning the value 10 to the variable y.


Why do we need comments?


Using comments in JavaScript has several important benefits. Here are some key benefits:

  1. Explaining source code: Comments help explain the source code in a clear and understandable way. When the development team works together or when you come back to the source code after a long time, comments will help you remember the original intention of the source code.
  2. Note about completed tasks: By using comments, you can note about completed tasks, issues to be addressed in the future, or significant changes in the source code.
  3. Temporarily disable source code: You can use comments to temporarily disable a part of the source code, but still keep it for future reference.
  4. Debugging and testing source code: Comments help you identify errors in the source code and verify the correctness of the code.




Read more
On This Page