Comments in JavaScript
In the Unicode Character Set blog, we have learned about what Unicode is. Unicode helps websites work well with different languages, making the online experience more connected and accessible. In coding, knowing and using Unicode is essential for creating websites that speak a global language. In this blog, we will learn about the Comments and how to use it in JavaScript.
What are Comments in JavaScript
In JavaScript, Comments are used to explain the code and make it more readable. For example, if we have some comments, some information, or some explanation of the code that we want to note down for future reference and we want it not to be a part of the code then we can write that information or code in Comments. We can write comments in two ways: Single Line Comment (//) and Multi-line Comment(/* */).
Ways of Adding Comments
As mentioned in the above section, we can write comments in two ways: Single Line Comment and Multiline Comment
Single-line Comments
In JavaScript, we can create single-line comments using two forward slashes //. Anything following // on the same line will be treated as a comment and ignored by the JavaScript interpreter. Single-line comments are typically used for short explanations or notes on a single line of code. Single-line comments can also be used to comment out the code. Let’s take a look at the example:
In the above example, in the first line, we have added a statement starting with two forward slashes i.e. //Assigns the value 10 to the variable is a comment and will be ignored by the JavaScript Interpreter. Same is the case with the //Print the value of the variable statement. Single-line comments can help us describe our code and make it more understandable to ourself and other developers who may be working on the codebase. Let’s take an example
Multi-line Comments
In JavaScript, you can create multi-line comments using the /* and*/ characters. Anything enclosed between /* and */ will be treated as a comment and ignored by the JavaScript interpreter. Multi-line comments are suitable for longer explanations or for commenting out multiple lines of code. Let’s take a look at the example
In the above example, multiple lines are added at the start of the code which will be considered as a multi-line comment and will be ignored by the JavaScript Interpreter.
Comments in JavaScript are like helpful notes for developers, explaining or annotating code without affecting how the program runs. They enhance human understanding and serve as documentation.