In this lesson, we will learn how we can comment on code in JavaScript. Comments in JavaScript make code easier to read and understand by providing hints that a programmer can add.
There are two different ways to write comments in Javascript:
- Single Line Comments
- Multi Line Comments
Single Line Comments
SIngle line comments starts with two forward slashes( // ). All text after the two forward slashes until the end of a line makes up a comment, even when there are forward slashes in the commented text. For example,
const name = "Jack";
// printing Hello Jack on the console
console.log("Hello " + name);
Here, // printing Hello Jack on the console
is a comment.
You can also use single line comment like this:
const name = "Jack"; // Jack value is assigned to the name variable.
const test = "hello world"
//console.log(test)
Here, // console.log(test)
does not log the value of the test in the console due to the two forward slashes.
Multi Line Comments
Javascript multiline comments, also known as block comments, start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). They do not require a comment delimiter character on every line and may contain new lines. For example,
/* This is a multi line comment example.
You can have multiple line of text here.
*/
Conclusion
In this lesson, you have learned how about JavaScript comments.