Open In App

JavaScript Comments

Last Updated : 15 Nov, 2024
Suggest changes
Share
Like Article
Like
Report

Comments help explain code (they are not executed and hence do not have any logic implementation). We can also use them to temporarily disable parts of your code.

1. Single Line Comments

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

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

Output
Hello Geeks! 

2. Multi-line Comments

A multiline comment begins with /* and ends with */

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 in HTML

Prerequisite : How to Add JavaScript in HTML Document?

The syntax of comments remains same when we write inside HTML script tag.

HTML
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script>  // Prinintg Hello World  document.write('Hello World');  </script> </body> </html> 

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.

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 

Next Article

Similar Reads