Friday, June 8, 2012

Java Script Beginner Tutorial 4 : Java Script Comments

JavaScript comments can be used to make the code more readable. Comments are not executed by JavaScript. There are two ways for commenting JavaScript Code. These are as follows


Single line comments :
Single line comments start with //. For example, we use single line comments to explain the code.


// Edit paragraph on page load
document.getElementById("Parag").innerHTML="This paragraph added using JS.";


Single line comment can be used to prevent the execution of one of the code lines, for example


//document.getElementById("p1").innerHTML="paragraph 1 text here";
document.getElementById("p2").innerHTML=" paragraph 2 text here ";


First code line 'll not be executed by browser.

JavaScript Multi-Line Comments :
Multi line comments start with /* and end with */.
For example


/*
The JS code below will 
Edit paragraph,
having id "parag".
*/
document.getElementById("Parag").innerHTML="This paragraph added using JS.";


Multi-line comments can used to prevent the execution of a code block. For example


/*
document.getElementById("p1").innerHTML="paragraph 1 here";
document.getElementById("p2").innerHTML=" paragraph 2 here ";
*/


Comments can be placed at the end of a code line as well. For example


var str="My name is Waqas."; // declare a variable and assign a value to it
str= str + "I am JS developer"; // concatenate string to variable.

No comments:

Post a Comment