JavaScript Comments

Comments are an essential part of any programming language, and JavaScript is no exception. Comments are used to explain code, provide information about the code, or to prevent code from being executed. In this tutorial, we will learn how to use comments in JavaScript to make our code more readable and maintainable.

Why should you use it?

  • Comments make code more readable and understandable.
  • Comments help to maintain code by providing information about the code.
  • Comments can be used to disable code for debugging and testing.

Comments

Comments are lines of code that are not executed by the browser. They are used to add notes, descriptions and explanations to code. Comments are ignored by the browser. This makes it easier for humans to read and understand code. JavaScript supports two types of comments: single-line and multi-line comments.

Single-line Comments

Single-line comments are used to add a single line of comment to the code. They start with two slashes (//) and continue until the end of the line.
// This is a single-line comment
console.log('This code will execute');

Multi-line Comments

Multi-line comments are used to add multiple lines of comment to the code. They start with a slash and an asterisk (/*) and end with an asterisk and a slash (*/).
/*
This is a 
multi-line comment
*/
console.log('This code will execute');

Commenting Out Code

Comments can also be used to "comment out" code. This means that code is temporarily disabled so that it does not execute. This can be useful for debugging or for temporarily disabling code. To comment out code, you can use a single-line comment or a multi-line comment.
// This code will not execute
console.log('This code will not execute');

/*
This code will not execute
console.log('This code will not execute');
*/

console.log('This code will execute');
1