Comments are those lines within a program or script that the compiler or interpreter does not read when they compile or interpret any source code. In this chapter, you will learn about two different types of comments that the JavaScript programmer allows to use.



There are two ways to include comments in JavaScript.

ECMA-Script standardizes for JavaScript the C-style comments for both single-line comments as well as block comments. The single-line comment starts with two forward-slash characters, such as:

Syntax:

// comment applied for one line

A JavaScript block comment begins with a forward slash and asterisk (/ *) and ends with the opposite (* /), as in the example below:

Syntax:

/*

* This is the way of using multiline

* Comment...

*/

It is to be noted that even though the second and third lines contain an asterisk, these are not necessary and are included in the above code for clear readability.

Example:

<script>
//This is a single line comment.
/*JavaScript program for Comments.*/

document.write("Comment on my JavaScript code!");
</script>

Output:

Comment on my JavaScript code!


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram