JavaScript: Comments

Favourite

Comment is the description of the code and used to prevent the execution of a code block. It’s one of the first things coder have to learn. It is used to make the code more readable. It's important best practices when writing code. All professional code should have comments. It is not easy to understant the logic behind the code without comments.

JavaScript comment syntax is the same as in C,C++,Java and many other languages. JavaScript supports two styles of comments. Single line comments and multi line comments.

Single-line comments

Single-line comments are typically used to document short description that fit on a single line. This style comment start with // followed by the description.

// This is a single-line comment.
var is_expired = false; // set to true when expired.

//Following code will show the dialog box to say hello.
alert("Hello World!");

Multi-line comments

Multi-line comments are typically used to document major functions or code blocks. This style comment start with /* and end with */.

/* This is multi-line comments */

/*
* This is comment line one.
* This is comment line two.
*/

/*
* Following code will show the dialog box to say hello.
* "name" is the name of the person who you want to say hello.
*/
function SayHello(name)
{
	alert("Hello " + name);
}
Both comment styles may be mixed in same program but should never be nested.



author's photo

Author : Blue Cloud

Just a normal software developer who love to write code. Developing professional software since 1999. After working in the software industry for many years, I've started my own website to share knowledges and experiences.

COMMENTS
blog comments powered by Disqus
Contributors