Javascript Basics

Javascript Single Line Comment and Multiline Comments

Comments help us to keep note inside the script file which later on helps the coder what are the actual purpose of the code. 

What is a comment in Javascript?

In javascript, comment is a hint text or note text a developer or coder include in the script file, so that it would help him to recall the real purpose of the code or any importance regarding the code. 

Example

Suppose a company hired you to develop a login credential validation script. You are a freelancer. You did that. And your script has lets say 5000 lines. One year later, the same company asked you for modification. Because you are a freelancer and you might have done hundreds of projects in the span of one year, it is obvious you might not have remembered why you written the codes the way it is today.

So to handle such situation, we include comments inside the script file.

Comment is simple text line which has no impact or influence to the rest of the codes in the file. 

How do we define comments in Javascript?

In javascript, we can define two types of comment.

  • Single Line Comment
  • Multiple Line Comment

Single Line Comment

As the name says, it is one line text. 

Syntax

Single line comment – Single text line begins with double forward slash. (//)

Example

<script>

// below code calculates the age of the employee

var employee_id=document.getElementById(’emp_id’).innerText;

var getDOB=GetEmployeeDOB(employee_id);

var employee_age=CalculateAge(getDOB);

alert(“Employee Age: “+employee_age);

</script>

Ignore the code at this moment. The first line in the above example is a single line comment. And it hints the purpose of the below codes.

Multiple Line Comments

Multiple Line comment as name says – text lines more than one

Syntax

We keep more than one comment lines in between “/*” and “*/”

Example:

<script>

/*

To get the age of the employee first fetch the employee id from the form.

Then using predefined function GetEmployeeDOB, get employee’s Date of birth.

Then using predefined function, calculate age of the employee.

*/

var employee_id=document.getElementById(’emp_id’).innerText;

var getDOB=GetEmployeeDOB(employee_id);

var employee_age=CalculateAge(getDOB);

alert(“Employee Age: “+employee_age);

</script>

In the above example, all the comment lines are kept in between “/*” and “*/’

Note: Instead of multiple comment markers – (“/*”,”*/”) we can also use single comment lines for multiple times. In the above example, three comment lines are kept in between, “/*” and “*/”. Alternatively, we can also declare three single comment lines.

Example:

<script>

// To get the age of the employee first fetch the employee id from the form.

// Then using predefined function GetEmployeeDOB, get employee’s Date of birth.

// Then using predefined function, calculate age of the employee.

var employee_id=document.getElementById(’emp_id’).innerText;

var getDOB=GetEmployeeDOB(employee_id);

var employee_age=CalculateAge(getDOB);

alert(“Employee Age: “+employee_age);

</script>

In the above example, alternatively we have declared three single line comments

It all depends on developer or coders personal choice.