• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

JavaScript-Comments question


Senior Member
Alright, so from my tutorials so far I am now on JavaScript basics. My question is about comments. To sum up what the tutorial site says on the subject, they are used to "explain the JavaScript and make the code more readable" Any other observations about this?

So, I know if you use comments before a line of code, the comments prevent the script from executing;

Single line: <script type="text/javascript">
//document.write("<h1>User defined text</h1>");

However, it also covers placing these comments after a line of code. But I don't see what this does, because the only thing that gets displayed is what is between the parentheses after "document.write"

After a line: <script type="text/javascript">
document.write("Hello"); // Write "Hello"


Staff member
Assuming you mean comments in the javascript file to describe what each bit is.

In my experience - with all the javascript (downloads) I've used on my site theres always been a version without comments which is used on the site or is for more experienced people.

Comments are usually there to point us in the right direction, ensure things are changed where needed etc when its all being set up and would be removed prior to uploading so as to save bandwidth (Jaz will no doubt have a hey day on this one :))

Comments don't have any effect on the actoion of the javascript code.


Senior Member
Ahh, thank you Levi. I think I see what you mean. But I won't mind if Jaz wants to chime in and share knowledge, or anyone else. At this point anything else I can learn is much appreciated.


Active Member
Why heeeeellllllloooooooo ppl, may of had a tad, new year so yey, ahppy new year. :)
Anyhoo... a comment be it in Javascript, XHTML, PHP or CSS is a way to tell either yourself or other members of your team what a section of code is for.
For example ~

alert("Why hello Rhonda how are we?"); //This is to pop up a box asking a certain question to a certain indivdual named Rhonda. :)

Now the comments are literally that they are a way of you commenting on the code they do not get partsed for code, so if you wrote

//alert("Why hello Rhonda how are we?");

It would be ignored.

Now as Levi says -
when its all being set up and would be removed prior to uploading so as to save bandwidth
Top marks to that man. :clap:

Because as comments are ignored they don't need to be on a live site, all they do then is add bytes to the page which the browser has to spend time downloading thus adding slightly to your page load time and thus bandwidth and in some circumstnaces as they are used to make the code more easier to be understood for us mere mortals, can provide valuable intel to hackers.

<!--As in the root password is set to - I don't believe it, and the login user name is set to - your a dumbass-->

So they need to be removed unless they have to be left in due to someone else writting the code and needing credit for their amazingness.

For example on the, how do ppl know I'm about to link to my own thread here, how to make a HTML contact form sticky I wrote I placed at the bottom ~
<!--Contact form curtsy of Jazajay at designforums.co.uk-->

As that then tells users that 1 I wrote it and 2 if they want it check out designforums for the code.

Hope that makes seome sense and happy new year to allllll. :D
Jazzy Jaz Jaz

Brown - JavaScript
Orange - Comment
Grey - HTML comment


Senior Member
Thank you very much Levi and Jaza! You both told me way more then the tutorial site did about it. And I bet you both could tell more about other parts to JavaScript.;)

I think I get it now. So comments are optional, unless they need to be in there(despite adding up bytes, and adding security risk) . Like maybe if you have a site for a company or corporation that multiple coders actually maintain. It might be needed to use comments in certain sections to alert other coders not to change this specific part or to modify this section for whatever reason.


Active Member
Yeah thats pretty much it, shouldn't be on a live site though unless they have to be for copyright reasons. Bare in mind on the contact form thread I don't require it to be there, just nice if you, or anyone, wants to use it, I wont go nuts if you, or anyone, doesn't keep it in if you get me.
It was just best example I could think of at the time.

Hope you had a good new year. :)


Active Member
If bandwidth is a concern and the site has high traffic then it may make a differance if you use other techquies. But if they are not needed TBH why add the extra bytes?