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:
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.
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.
Using comments in JavaScript has several important benefits. Here are some key benefits: