Comments in Javascript

Comments in Javascript

  • Comments are very useful in Javascript.
  • It is useful in debugging the code and it makes very easy to find bugs, hence making code error free.
  • Comments can be added using // and /**/
  • For Single line comment, we can use the double back slash //.
  • For Multi line comment, we can use the combination of backslash and asterisk /**/.
  • The Javascript Interpreter ignores the commented lines and moves to next line or block of code.

Have a look at the Sandbox IDE for better understanding.



/* Example to show how to add comments an
   how javascript engine or interpreter handles
   the lines commented.
*/


//finding the factorial of given number. e.g: 5
function factorial(n) {
  //if number is 1 or 0 then return 1
  if (n === 0 || n === 1) return 1;
  //else return n*factorial(n-1)
  return Number(n * factorial(n - 1));
}


console.log(factorial(10));


/*  Here it can be seen that i've added comments to explain
    the lines of code.
    The interpretter ignores the lines commented and
    just moves to next line/block of code.
*/

        

To view or add a comment, sign in

More articles by Sourav Kumar

  • Introduction to Javascript

    Javascript is a high-level object-oriented programming language. It is one of most used programming languages.

Insights from the community

Others also viewed

Explore topics