×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

Comments in JavaScript

JavaScript comments: In this tutorial, we will learn about the comments in JavaScript and types of the comments (single and multi-line comments) with the help of examples. By IncludeHelp Last updated : July 29, 2023

JavaScript Comments

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.

Types of JavaScript Comments

There are two types of comments in JavaScript -

  1. Single-line comment
  2. Multi-line comments

1. 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

2. 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

JavaScript Tutorial »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.