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.
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.
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 (*/).
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.
console.log('This code will not execute');
console.log('This code will execute');