Skip to main content

Comments in JavaScript

In this tutorial, you will learn about comments in JavaScript. Comments are used to explain the code and make it more readable. They are ignored by the JavaScript interpreter.

There are two types of comments in JavaScript:

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

Single-line Comments​

Single-line comments start with //. They are used to add comments to a single line.

For example,

single-line-comments.js
// This is a single-line comment

console.log("Hello, World!"); // Output: Hello, World!

Multi-line Comments​

Multi-line comments start with /* and end with */. They are used to add comments to multiple lines.

For example,

multi-line-comments.js
/*
This is a multi-line comment
It can span multiple lines
*/

console.log("Hello, World!"); // Output: Hello, World!

In this example, the multi-line comment spans multiple lines.

Now Let's Get Started with html

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comments in JavaScript</title>
</head>
<body>
<h1>Comments in JavaScript</h1>
<p>Open the console to see the output.</p>
<script>
// This is a single-line comment
console.log("Hello, World!"); // Output: Hello, World!
</script>
</body>
</html>

In this example, we have added a single-line comment and a single-line comment at the end of the line.

Now Let's Get Started with multi-line comments

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comments in JavaScript</title>
</head>
<body>
<h1>Comments in JavaScript</h1>
<p>Open the console to see the output.</p>
<script>
/*
This is a multi-line comment
It can span multiple lines
*/
console.log("Hello, World!"); // Output: Hello, World!
</script>
</body>
</html>

In this example, we have added a multi-line comment that spans multiple lines.

note

Comments are used to explain the code and make it more readable. They are ignored by the JavaScript interpreter.

Conclusion​

In this tutorial, you learned about comments in JavaScript. You learned about single-line comments and multi-line comments. Comments are used to explain the code and make it more readable. They are ignored by the JavaScript interpreter.