Open In App

JavaScript Comments

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript comments are used to explain the code to make it more readable. It can be used to prevent the execution of a section of code if necessary. JavaScript comments are ignored while the compiler executes the code.

Single Line Comments

A single-line comment in JavaScript is denoted by two forward slashes (//),

Syntax:

// your comment here

Example 1: This example illustrates the single-line comment. 

Javascript




// A single line comment 
console.log("Hello Geeks!");


Output

Hello Geeks!

Example 2: In this example, we will assign values to some variables and explain them with single-line comments.

Javascript




// Declaring a variable and assign value to it
let geek = 'Computer science portal';
console.log(geek)
 
// Perform operation of addition of two numbers
let sum = 5 + 8
console.log(sum)


Output

Computer science portal
13

Multi-line Comments

A multiline comment in JavaScript is a way to include comments that span multiple lines in the source code.

Syntax:

/*    
This is a multiline comment
It can span multiple lines
*/

Example: This example illustrates the multi-line comment using /* … */ 

Javascript




/* It is multi line comment. 
It will not be displayed upon
execution of this code */ 
 
console.log("Multiline comment in javascript");


Output

Multiline comment in javascript

JavaScript Comments to Prevent Execution

We can use // or /*…*/ to change the JavaScript code execution using comments. JavaScript Comments are used to prevent code execution and are considered suitable for testing the code.

Example 1: JavaScript comments are used to prevent execution of selected code to locate problems of code or while testing new features. This example illustrates that commented code will never execute. 

Javascript




function add() {
    let x = 10;
    let y = 20;
    let z = x + y;
    // console.log(x + y);
    console.log(z);
}
 
add();


Output

30

Example 2: This example uses multi-line comments to prevent the execution of addition code and perform subtraction operations.

Javascript




function sub() {
    let x = 10;
    let y = 20;
    /* let z = x + y;
    console.log(z); */
 
    let z = x - y;
    console.log(z);
}
 
sub();


Output

-10


Previous Article
Next Article

Similar Reads

How to auto like all the comments on a facebook post using JavaScript ?
In this article, we are going to learn how to like all the comments on a Facebook post automatically. Many times we want to like all the comments of friends on our post or on any other's post, so this script helps us to save time and effort by automatically liking all the comments on the post. Approach: Make variable comments that points to an arra
2 min read
Design a Nested Chat Comments using HTML CSS and JavaScript
In this article, we will learn how to create Nested Comments using JavaScript. We must have seen it on social media like Facebook, Instagram, Youtube, Twitter, etc. We have seen the use of the nested comment in the comment section of these social sites. Approach: Create a folder nested-comments on your local machine and go to that folder.Create 3 f
2 min read
How to write comments in PHP ?
In this article, we have to pass a comment for PHP code by using some simple basic syntax. Comments are helpful to understand the complex code. The comment tag is useful during the debugging of the codes. We use comments to understand the functionality of the code. Basically, the single-line comments begin with // comments. Example 1: C/C++ Code
1 min read
SASS | Comments
Comments in SASS is very similar to the comments in any other programming language. But the difference comes when we talk about the type of comments will be there in the compiled CSS file. There are two types of comments in Sass: Silent Comments: Silent comments are single-line comments. When we compile Sass file these comments don't reflect in com
1 min read
How to block comments in YAML ?
YAML is a human-friendly data serialization standard for all programming languages. It is commonly used for configuration files and in applications where data is being stored or transmitted. The Normal Way for commenting in YAML is Inline commenting with the "#" symbol, however, if you want to comment blocks then we have a list of ways in which it
2 min read
How to write comments in HTML ?
In this article, we have to pass a comment for an HTML by using <!-- Comment --> tag. In html, we can add single-line comments and multi-line comments. These are used to insert comments in the HTML code. It is helpful to understand complex code. The comment tag is useful during the debugging of the codes. Syntax: Single line comment <!-- C
1 min read
YAML Comments
[caption width="800"]YAML_Comments[/caption] Comments are used to prevent the execution of statements. Comments are ignored while the compiler executes the code. Comments are user-friendly as user can get explanations of code using comments. Since you are alright with the sentence structure and fundamentals of YAML, let us continue further into its
3 min read
Integrating Facebook Comments Plugin in Django Project
Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. In this article, we will learn to integrate the Facebook comment plugin in Dj
2 min read
Documentation Comments in JSDoc
In JSDoc we need to include documentation comments in the code through which JSDoc will generate an HTML documentation website. Let's see how to create documentation comments for different types of code. String, Numbers & Arrays: C/C++ Code /** * Site Name * @type {string} */ const siteName = "GeeksForGeeks"; /** * Number * @type {num
4 min read
How to Disable Comments in WordPress ?
Visitors to your website can interact with you and each other through comments. WordPress puts multiple text boxes after your content where readers may leave their comments when you enable comments on a Page or post. When you accept a remark, it will show below your content. WordPress offers a comprehensive range of choices for building a community
3 min read