• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JScript » Tutorial » JavaScript Comments
Next →
← Prev

JavaScript Comments

By Dinesh Thakur

JavaScript Comments or ( JS Comments ) are very useful and practical when developing code. It is possible to comment out a specific line or a block of code with JavaScript Comments.

You may need to make comments on code, such as to describe what a code is supposed to do. It’s also possible that you will want to disable a line of the script for some reason in webpage. For instance, if you are looking for an error in a script.

You can accomplish these tasks in Netscape JavaScript by using JavaScript comments. You can insert JavaScript comments that appear on one line Comment or run for numerous lines comments.

Fortunately, most programming languages provide a way for programmers to leave comments for themselves or other programmers who might look through their Script. A JavaScript comments is simply a line or more worth of comments: the Netscape JavaScript interpreter ignores them.

We’ll be covering the following topics in this tutorial:

  • Creating Single Line Comments
  • Multiple-Line Comments

Creating Single Line Comments

If you want to add comment on a single line in your script, place a pair of forward slashes before the text of the comment:

// Your Code is here 

In this format, anything preceding the two slashes on that line is “live” Java Script webpage that will be executed and anything after the slashes on that line is ignored. For example, suppose that you wrote this line in your comments script:

document.write("This is cool! web page");  // Comment writes out Here 

The document.write() method will be run by the web browser, so the text “This is cool! web page” will be written to the web page. However, the Java Script comment after the slashes will be ignored by the web browser. If you place the forward slashes at the beginning of a comment, the browser will ignore the entire line. Suppose that you move the slashes in the previous example to be the first items on the line:

// document.write("This is cool! web page");  Comment writes out Here 

In this format, the entire line is ignored by Java Script, since it begins with the two slashes that represent a Java Script comment. The text will not be written to the web page, since the script will not be executed by the web browser. In effect, you are disabling the document.write() statement. You may wish to do this if the Java Script containing this line has an error and you want to know whether or not this line is causing the problem in script.

Multiple-Line Comments

Java Script Comments denoted by a pair of forward slashes apply only to the line on which they appear. To add Java Script comments that span any number of lines in script, you use a different comment format: a forward slash followed by an asterisk at the beginning of the comment, then the text of the comment, and then an asterisk followed by a forward slash at the end of the comment. Here’s an example:

/* 
My Comments Code will write here 
All of this JavaScript Comments text is ignored by the web browser. 
*/ 
document.write ("You can see Comment "); 

Using this format, you can begin the comment in one line and end it on another line. Multiple-line comments can be handy when you want to insert lengthier descriptions. Look at this Multiline Comments example here.

<script type="text/JavaScript">
/*
This Multiline Comments code won’t work for some reason.
document.write ("Multiline Comments code");
</script> 

Did you notice that the closing comment symbols are missing?  In above example, the comment just keeps going on with no end in sight. To fix this Comments, you need to close the comments before the document.write () method is used:

<script type="text/javascript"> 
/* The comment code is now working! This text is hidden. You need to close the comments before the document.write () method */ 
document.write("Now everyone can see Comments code ! "); 
</script> 

You’ll also like:

  1. Comments in C++
  2. What are Comments in Java? – Definition
  3. How many Types of Comments are there in PHP
  4. JavaScript Operators | Types of JavaScript Operators
  5. JavaScript statements
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

JavaScript Tutorials

JavaScript Tutorials

  • JavaScript - Home
  • JavaScript - Operators
  • JavaScript - Comments
  • JavaScript - If...Else
  • JavaScript - For Loop
  • JavaScript - Popup Boxes
  • JavaScript - Switch Statement
  • JavaScript - while Loop
  • JavaScript - Functions
  • JavaScript - Variables
  • JavaScript - Statements
  • JavaScript - Substring()
  • JavaScript - indexOf()
  • JavaScript - Match()
  • JavaScript - Replace()
  • JavaScript - Search()
  • JavaScript - Split()
  • JavaScript - Substr()
  • JavaScript - Blink()
  • JavaScript - Anchor()
  • JavaScript - Big()
  • JavaScript - Strings Object
  • JavaScript - String Format
  • JavaScript - Length Property
  • JavaScript - Prototype
  • JavaScript - Slice Substr Vs Substring
  • JavaScript - JavaScript:void(0)

Other Links

  • JavaScript - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW