Home

How to Make Comments in JavaScript

|
Updated:  
2016-03-26 07:26:34
|
JavaScript Essentials For Dummies
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
   Holland.
*/

About This Article

This article is from the book: 

About the book author:

Chris Minnick is an accomplished author, teacher, and programmer. Minnick authored or co-authored over 20 books, including titles in the For Dummies series. He has developed video courses for top online training platforms and he teaches programming and machine learning to professional developers at some of the largest global companies.

Eva Holland is an experienced web developer, tech trainer, and coauthor of Coding with JavaScript For Dummies. She is a co-founder of WatzThis?, a company focused on training and course development.