Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

JavaScript Comments

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Javascript comments explain the code design. They can also be used to prevent the execution of a section of code if necessary. Comments are ignored while the compiler executes the code. Comments are user-friendly as users can get explanations of code using comments. 


// For single line comment
/* For block of lines comment

Return Value: During the execution of code, comments are ignored. 

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


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

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


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

Example 3: Javascript comments can be used to prevent selective execution of code to locate problematic code or while testing new features. This example illustrates that commented code will never execute. 


<!DOCTYPE html>
<html lang="en">
    <label>Enter the first number
        <input id="num1">
    <label>Enter the first number
        <input id="num2">
    <button onclick="add()">
    <input id='sum'>
        function add() {
            let x, y, z;
            x = Number(document.getElementById("num1").value);
            y = Number(document.getElementById("num2").value);
            z = x + y;
            // Comment the code section
            //document.getElementById("sum").value = z;



Explanation: The sum is calculated but not displayed because the code to display the sum is written in a comment and will not be displayed 

My Personal Notes arrow_drop_up
Last Updated : 22 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials