Comments in JavaScript

Comments in JavaScript
Comments are just that, comments that you put in your JavaScript or HTML to explain what is going on. There are several ways to put comments in your JavaScript code. Comment syntax – how to tell the script or document that something is a comment and not data or code – is very simple. Writing good comments is harder, and from looking at code (my own included), the hardest part seems to be actually doing it!

JavaScript has 2 types of comments, a single line comment and a multi-line comment.

The single line comment in JavaScript starts with two forward slashes (//) and ends at the end of the line (technically, immediately before the line terminator character). Single line comments are particularly useful for short notes.

// This is a single line comment
data="my data" // This is a single line comment too

A multi-line comment in JavaScript starts with a forward slash followed by an asterisk (/*) and ends with an an asterisk followed by a forward slash (*/). Everything between the /* and */ is considered a comment. This type of comment can span multiple lines. It is also possible to put this type of comment in the middle of a line of code, if it does not span multiple lines, but this is rarely useful.

/* this is line one of a multi-line comment
this is still part of my comment
this is the last part of my comment */

/* this is a multi-line style comment on a single line */

You often see extra asterisks added to multi-line comments to create decorative information boxes, such as:

* My Functions *

Multi-line functions are particularly useful at the beginning of a program or section of a program to describe what the program is intended to do and how it does it.

Comments are used for two purposes. As you write code (or even before you write it!), you should write comments that will allow another person to understand what the program is doing (or supposed to be doing.) Even if you don't expect anyone else to ever read your code, you'll be surprised how much you can forget if you want to make changes after a few weeks or months.

Also, particularly in scripting languages like JavaScript, comments are often used as part of the debugging process. You can put in code to use for debugging and comment them out when you are done using them or comment out sections of code that you think are causing problems or are changing to add new functionality. When you do this, be particularly careful not to put a single line comment on the line in front of the beginning of a multi-line comment. This uncomments the rest of the multi-line comment, which will almost certainly cause an error.

This site needs an editor - click to learn more!

You Should Also Read:
A First JavaScript Program
JavaScript Beginners Resources
JavaScript/Java Newsletter

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.