Welcome to the Javascript Comments Homepage, Guest!


Javascript | Comments




Comments

Javascript comments can be used to explain the javascript code you're writing to yourself or others. The comments help make the code more readable. Comments can always be used to prevent execution, while testing alternative code.




Single Line Comments

A single line comment can be represented by two slashes, and to the end of the line will not be executed.

// this is a comment
blah(blah) = blah;
// this is the second comment
blah2(blah2) = blah2;

You could use a comment following a line of code to explain the line instead of placing the comments on top of the line of code

blah(blah) = blah; // this is a comment

blah2(blah2) = blah2; // this is the second comment




Multiple Line Comments

Multi-line comments will start with a /* and end with a */. All of the text between the two tags will be ignored by Javascript. Multi-line comments are often used for documentation within the HTML document. I use multi-line comments to store useful elements I use in my everyday programming of HTML. The example below shows how to use multi-line comments correctly:

/*
the comments placed here will be ignored
the heading with id = myID
will change once the site is accessed.
*/
document.getElementById("myId").innerHTML = "text to replace";



Prevention

Comments can be used to prevent execution of code. For instance, you're working on a function, but have another idea of how to achieve the goal. Comment out one of the functions while you test the other one. The old one might have a great part or two, and the new one might have excellent other parts, and together they might create a much better function. The code below comments out the call to myId element to change the text.

blah(blah) = blah; // this is a comment

// document.getElementById("myId").innerHTML = "text to replace";

blah2(blah2) = blah2; // this is the second comment

You could comment a whole block of code out quickly using block comments:

/*
blah(blah) = blah;

document.getElementById("myId").innerHTML = "text to replace";

blah2(blah2) = blah2;
*/




Next, we will take a look at variables in Javascript more closely; literals and variables.

Just follow the buttons to continue the tutorial, Guest