Home » JavaScript

Comments in JavaScript

JavaScript comments: Here, we are going to learn about the comments in JavaScript, types of the comments 1) single line comment, 2) multiline comments.
Submitted by IncludeHelp, on March 02, 2019

Like other programming languages, in JavaScript comments can be used to prevent the execution of the statements. Whenever we want to ignore the statements or any text/document from execution, we use comments.

For example: While writing the code, sometimes the logic is complex and we want to explain in our words so that we can understand the logic – we can put the explanation in the comments.

There are two types of comments in JavaScript:

  1. Single line comment
  2. Multi line comments

JavaScript single line comment

For single line comment in JavaScript, we use double slash characters (//) before the text/code. The code/text written after the double slash (//) will be ignored by the interpreter.

Syntax:

    //This text will not be executed/ interpreted

Example:

<script>
	document.write("This is line1<br>");
	//document.write("This is line2<br>");
	document.write("This is line3<br>");
	//document.write("This is line4<br>");
	document.write("This is line5<br>");
</script>

Output

This is line1
This is line3
This is line5

JavaScript multi line comment

Multi-line comments can be placed between /* and */. The comment starts with /* and terminates with */. We can place any number of lines between these comment characters.

Syntax:

    /* 
    This line will not be executed/ interpreted
    This line will also not be executed/ interpreted
    This line will also not be executed/ interpreted
    */

Example:

<script>
	document.write("This is line1<br>");
	/*document.write("This is line2<br>");
	document.write("This is line3<br>");*/
	document.write("This is line4<br>");
	/*
	document.write("This is line5<br>");
	document.write("This is line6<br>");
	document.write("This is line7<br>");
	*/
	document.write("This is line8<br>");
	document.write("This is line9<br>");		
</script>

Output

This is line1
This is line4
This is line8
This is line9

Comment the code in a statement

We can comment on any code in a statement by using multi-line characters.

Example:

<script>
	document.write("This is line1<br>");
	document.write("This is line2<br>");
	document.write(/*"This is line3<br>"*/"Hello world<br>");
	var result = /*a-b*/ 10-2;
	document.write("result: " + result + "<br>");
</script>

Output

This is line1
This is line2
Hello world
result: 8





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.