JavaScript For Kids For Dummies
Book image
Explore Book Buy On Amazon

JavaScript comments are a way that you can put text into a program that isn't a string or a statement. This may not sound so great, but the thing that makes comments so important and useful is precisely that they don't cause JavaScript to do anything at all.

Programmers use comments within their code for several reasons:

  • To tell their future selves, and anyone else who works on the program in the future, why they wrote something in the particular way they did

  • To describe how the code they wrote works

  • To leave themselves a note telling what they still need to do, or to list improvements that they intend to make at a later date

  • To prevent JavaScript statements from running

JavaScript has two different kinds of comments: single‐line and multi‐line.

  • Single‐line comments: Single‐line comments cause everything following them on the same line to be a comment. To create a single‐line comment, use two slashes (//) back to back. For example, here the first three lines are single‐line comments, and the fourth line contains a statement that will be executed, followed by a comment.

    // The following code won't run.
    // alert("Watch out!");
    // The next statement will run.
    alert("Have a nice day!"); // pops up a nice message
  • Multi‐line comments: Multi‐line comments are comments that can be more than one line long. To create a multi‐line comment, start with /* and end the comment with the exact reverse, */. Here is an example of a multi‐line comment.

  AlertMe, by Chris Minnick and Eva Holland
  A program to alert users that they are
  using a JavaScript program called AlertMe,
  which was written by Chris Minnick and Eva

About This Article

This article is from the book:

About the book authors:

Chris Minnick and Eva Holland are experienced web developers, tech trainers, and coauthors of Coding with JavaScript For Dummies. Together they founded WatzThis?, a company focused on training and course development.

This article can be found in the category: