The Wayback Machine - https://web.archive.org/web/20240920014719/https://www.geeksforgeeks.org/javascript-comments/
Open In App

JavaScript Comments

Last Updated : 26 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript comments help explain code, making it easier to understand. You can also use them to temporarily disable parts of your code. The JavaScript compiler ignores comments when running the code.

1. 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

2. 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

JavaScript Comments – FAQs

What are comments in JavaScript?

Comments are non-executable parts of the code used for explanations, documentation, and debugging.

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

Use // at the beginning of the line.

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

Enclose text with /* and */.

Can comments be nested in JavaScript?

No, nesting comments is not allowed and causes syntax errors.

Why are comments useful in JavaScript?

They enhance code readability, aid debugging, and document complex logic.

Can comments affect the performance of JavaScript code?

No, comments are ignored during execution but may increase file size.


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
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
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 details. In this part, we will perceive how
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
WordPress Delete Comments
WordPress is a tool and an open-source Content Management System that is totally based on PHP and MySql which is used to create a dynamic website. WordPress was written in PHP language by Matt Mullenweg. It is one of the most popular interfaces that allow users to customize and manage the website from its back-end content management system. To orga
2 min read
WordPress Moderate Comments
WordPress is a tool and an open-source Content Management System that is totally based on PHP and MySql which is used to create a dynamic website. WordPress was written in PHP language by Matt Mullenweg. It is one of the most popular interfaces that allow users to customize and manage the website from its back-end content management system. To orga
3 min read
WordPress Spam Comments
WordPress is an open-source Content Management System. It is basically based on PHP and MySql and it is used to create a dynamic website. WordPress was written in PHP language by Matt Mullenweg in 2003. Currently, it is one of the most popular interfaces that allow users to customize and manage the website from its back-end content management syste
2 min read
How to write comments in ReactJS ?
To write comments in React JS we use the javascript comments and JSX object. It helps in adding useful information about the code and comment-out and ignore the extra code in the program. Writing comments in React is done in these ways. Table of Content Using JavaScript commentsUsing JSX objectSteps to create React ApplicationStep 1: Create a React
3 min read
How to include Comments in CSS Code ?
In CSS, Comments can be added to provide explanations, notes, or annotations within the code. Comments are helpful for code documentation and collaboration, providing insights into the purpose or usage of certain styles. Note: Comments are ignored by the browser and do not affect the rendering of the webpage. There are two ways to include comments:
1 min read
WordPress Add Comments
WordPress is a powerful platform for building websites, and one of its best features is the ability to add comments. Allowing comments on your WordPress site can greatly increase user engagement and interaction. This article will show you how to easily add comments in WordPress, helping you to build a more interactive and lively community. By learn
5 min read
How to Delete All Comments on WordPress?
Comments are an integral part of a WordPress site, fostering interaction and engagement. However, there are moments when you might need to delete all comments, whether it's to start fresh, remove spam, or streamline your site's content. This article outlines several methods to accomplish this task, ensuring that you can take the path that best fits
3 min read
WordPress Edit Comments
WordPress is a popular platform for creating and managing websites, and it offers powerful tools for managing comments. Being able to edit comments in WordPress is crucial for maintaining the quality and relevance of discussions on your site. This article will guide you on how to easily edit comments in WordPress, ensuring your site remains profess
4 min read
How to approve comments automatically in WordPress ?
WordPress is a powerful tool for managing websites and blogs, and handling comments efficiently can greatly enhance user engagement. If you're tired of manually approving every comment, learning how to auto-approve comments in WordPress can save you time and streamline your workflow. This article will guide you through simple steps to set up automa
4 min read
Comments in Pug View Engine
Pug is a template engine for NodeJS and browsers to render dynamic reusable content. At compile time, the template engine compiles our Pug template code to HTML. Pug has many powerful features like conditions, loops, includes, etc. using which we can render HTML code based on user input or reference data. Pug also supports JavaScript natively, henc
3 min read
How to Disable Comments in WordPress ?
WordPress is a powerful platform that allows you to create and manage a website with ease. While comments can be a great way to engage with your audience, there are times when you might want to disable them. Whether you're looking to prevent spam, keep your site professional, or simply maintain control over the conversation, disabling comments is s
3 min read
CSS Comments
CSS Comments. Comments are essential for documenting code, providing clarity during development and maintenance. They begin with `/*` and end with `*/`, allowing for multiline or inline annotations. Browsers ignore comments, ensuring they don't affect the rendering of web pages. Syntax:/* content */Comments can be single-line or multi-line. The /*
3 min read
HTML Comments
HTML comments are very important for developers, providing a way to annotate code without affecting the rendered webpage. HTML Comments enhance code readability, facilitate collaboration, and can be used to temporarily disable code segments. Enclosed within <!-- and -->, HTML comments serve as invisible notes within the code. What are HTML Co
3 min read
Implement Comments Section in MERN Blogs and News Website
In the digital age, the way we consume news and stories has drastically changed. Blogs and news websites have become the go-to sources for information, offering readers a vast array of content at their fingertips. However, what truly sets these platforms apart from traditional media is the ability for readers to engage directly with the content thr
10 min read
JavaScript Course Understanding Code Structure in JavaScript
Inserting JavaScript into a webpage is much like inserting any other HTML content. The tags used to add JavaScript in HTML are <script> and </script>. The code surrounded by the <script> and </script> tags is called a script blog. The 'type' attribute was the most important attribute of <script> tag. However, it is no
4 min read
Introduction to JavaScript Course - Learn how to build a task tracker using JavaScript
This is an introductory course about JavaScript that will help you learn about the basics of javascript, to begin with, the dynamic part of web development. You will learn the basics like understanding code structures, loops, objects, etc. What this course is about? In this course we will teach you about the basics of the scripting language i.e) Ja
4 min read
JavaScript Course Loops in JavaScript
Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. For example, suppose we want to print “Hello World” 10 times. Example: In this example we will print the same things, again and again, to understand the work of Loops. <script> cons
4 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &&:(AND) Evaluates operands and retu
3 min read
JavaScript Course What is JavaScript ?
JavaScript is a very famous programming language that was originally started in the year of 1995 with the motive of making web pages alive. It is also an essential part of the web developer skillset. In simple terms, if you want to learn web development then learn HTML & CSS before starting JavaScript. HTML: HTML is used to create the structure of
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
JavaScript Course Functions in JavaScript
Javascript functions are code blocks that are mainly used to perform a particular function. We can execute a function as many times as we want by calling it(invoking it). Function Structure: To create a function, we use function() declaration. // Anonymous function function(){ // function...body } // function with a name function displayMessage(){
4 min read
JavaScript Course Conditional Operator in JavaScript
JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the code block inside it will be executed otherwise n
3 min read