Try the JavaScript Course for Free!

Comments in JavaScript – Tutorial

/ / JavaScript, Latest

Comments in JavaScript: Video Lessons

            This video lesson, titled “Single-line Comments,” shows you how to create comments in JavaScript consisting of a single line. The next video, titled “Multi-line Comments,” shows you how to create comments in JavaScript that consist of multiple lines. These video lessons are from our Introductory JavaScript course, titled “Mastering Introductory JavaScript Made Easy v.1.0.”

Comments in JavaScript: Overview

            Comments in JavaScript are primarily used to explain what the code is for or to make it more readable. Single line comments start with a double forward slash (//), but do not have an end tag. Any text that is written after (//) will be completely ignored by JavaScript and will not be executed or displayed by the browser.

Start “Tag”:  //
End “Tag”: None
Example:  // Write to element with ID of “line”.document.getElementById(“line”).innerHTML=”Are you sure?“;
Explanation: Labels JavaScript code so anyone viewing will know the statement is to write to an element with an ID of “line”.

Comments in JavaScript - Tutorial: A picture describing the use of single-line comments in JavaScript.

Comments in JavaScript – Tutorial: A picture describing the use of single-line comments in JavaScript.

            Multi-line comments in JavaScript start with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/). This allows for breaking your comments up into many lines, making it more readable for later editing. Just like single line comments, multi-line comments are ignored by JavaScript and will not be displayed by browsers.

Start “Tag”:  /*
End “Tag”:  */
Example: /* This is anExample of a

Multi-line comment.

*/

Explanation: Allows you to write comments across multiple lines that will be ignored by JavaScript and web browsers.

Comments in JavaScript: Instructions

  1. To add a single line comment, type: //
  2. On the same line enter the information you want as your comment.
  3. To add multi-line comments on the line you want your comment to start, type: /*
  4. On a new line start the comment you want to enter
  5. When your comment is complete, type: */
TOP