JavaScript Comments

Comments in JavaScript are used to explain code and make it more readable. They are ignored by the JavaScript engine and have no effect on how the code runs. They’re helpful for documentation, debugging, and collaboration.


What You'll Learn

In this guide, you'll learn about the different types of comments in JavaScript and best practices for using them.


Why Use Comments?

  • Code Explanation: Help describe what your code is doing.
  • Debugging: Temporarily disable code while testing.
  • Collaboration: Make code easier for teammates to understand.

Types of Comments in JavaScript

1. Single-Line Comments

In JavaScript, a single-line comment starts with //. Anything written after // on the same line will be ignored by the browser when the code runs. This is useful when you want to leave a short note or explanation in your code.

javascript
// This is a comment. It will not run.
console.log("Hello, world!"); // This prints a message to the console

How It Works:

  • // tells JavaScript to ignore everything after it on that line. It's only for human readers, not the computer.
  • console.log("Hello, world!") is a command that prints text to the console. It's helpful for checking your output while coding.

Output

Hello, world!

2. Multi-Line Comments

Multi-line comments in JavaScript are used when you want to write longer notes that span more than one line. They start with /* and end with */. Everything between these symbols is ignored by JavaScript when the code runs.

javascript
/*
This is a multi-line comment.
You can write as much as you want here.
It won't affect your code.
*/
console.log("Hello, world!");

How It Works:

  • /* */ lets you add comments that take up multiple lines. This is great for writing longer explanations or temporarily hiding blocks of code.
  • console.log("Hello, world!") is the part of the code that actually runs and prints a message to the console.

Output

Hello, world!

Exercises

Try the following exercises to practice working with comments in JavaScript.

1. Write a program that logs your name and includes a comment explaining what the code does.
javascript
// Print your name
console.log("Your Name");

2. Write a program that includes a multi-line comment describing the purpose of the program.
javascript
/*
This program prints a welcome message.
It demonstrates how to use console.log in JavaScript.
*/
console.log("Welcome to JavaScript!");

3. Write a program that disables a line of code using a comment and runs the remaining code.
javascript
// console.log("This line is disabled.");
console.log("This line is executed.");

*Tip: After completing an exercise, experiment by adding or removing comments to better understand their use.


Frequently Asked Questions

What are comments in JavaScript?

Comments in JavaScript are notes or explanations added to your code. They are ignored by the browser and do not affect how the program runs. Comments are helpful for making your code more understandable.


How do you write a single-line comment in JavaScript?

A single-line comment starts with two forward slashes //. Everything after // on that line will be treated as a comment and not executed.


How do you write a multi-line comment in JavaScript?

A multi-line comment in JavaScript starts with /* and ends with */. You can write as many lines as needed between these symbols, and they will all be ignored during execution.


Why should you use comments in your code?

Using comments helps explain what your code does, which is useful for other developers reading your code or for yourself when reviewing it later. Comments also help during debugging or testing by temporarily disabling parts of the code.


Do comments affect the performance of JavaScript code?

No, comments are completely ignored by the JavaScript engine during execution, so they do not affect performance or output in any way.



What's Next?

Next, you'll learn about Type Conversion in JavaScript. Type conversion is the process of changing a value from one data type to another — for example, turning a number into a string or a string into a number. It's an important concept that helps your code work smoothly when dealing with different types of data.