[Solved] How to write comments in JavaScript

Use the syntax “// text” and “/* text */”

Comments in CSS are typically used to explain the purpose of the style rules declarations. It will help you and others to understand what you were trying to do with the style rules at the time of editing style sheets. Comments are not displayed by the browsers.

Single-line comments

JavaScript single line comment begins with //, See the example below:

<script type="text/javascript">
function helloWorld(){ alert("Hello World!");
{
// alert "Hello World!" in the browser
}
</script>

Multi-line comments

JavaScript multiple line comment begins with /*, and ends with */.

<script type="text/javascript">
/* The function below display an alert dialog with "Hello World!" message */
function helloWorld()
{
alert("Hello World!");
}
</script> 
Asked on October 12, 2018 in Javascript.
Add Comment


  • 4 Answer(s)

    1.Single line comment:To create a single line comment in JavaScript, you place two slashes “//” in front of the code or text you wish to have the JavaScript interpreter ignore. When you place these two slashes, all text to the right of them will be ignored, until the next line.

    These types of comments are great for commenting out single lines of code and writing small notes.

    JavaScript Code:

    Display:
    I have comments in my JavaScript code!

    Each line of code that is colored red is commented out and will not be interpreted by the JavaScript engine.

    2.creating multi-line comments
    Although a single line comment is quite useful, it can sometimes be burdensome to use when disabling long segments of code or inserting long-winded comments. For this large comments you can use JavaScript’s multi-line comment that begins with /* and ends with */.

    JavaScript Code:

    Display:
    I have multi-line comments!

    Answered on February 5, 2019.
    Add Comment

    Single line comments start with //.

    Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

    This example uses a single-line comment before each code line:

    Example
    // Change heading:
    document.getElementById(“myH”).innerHTML = “My First Page”;

    // Change paragraph:
    document.getElementById(“myP”).innerHTML = “My first paragraph.”;
    This example uses a single line comment at the end of each line to explain the code:

    Example
    var x = 5; // Declare x, give it the value of 5
    var y = x + 2; // Declare y, give it the value of x + 2
    Multi-line Comments
    Multi-line comments start with /* and end with */.

    Any text between /* and */ will be ignored by JavaScript.

    This example uses a multi-line comment (a comment block) to explain the code:

    Example
    /*
    The code below will change
    the heading with id = “myH”
    and the paragraph with id = “myP”
    in my web page:
    */
    document.getElementById(“myH”).innerHTML = “My First Page”;
    document.getElementById(“myP”).innerHTML = “My first paragraph.”;

    Answered on February 9, 2019.
    Add Comment

    Single line comments start with //.Multi-line comments start with /* and end with */.

    Any text between /* and */ will be ignored by JavaScript.

    Answered on February 18, 2019.
    Add Comment

    You can write comments in Javascript using the symbols // and /**/. If you want to make single line commented, you have to put the symbol double slash(//) at the start of the line. A multiline comment can be done by using the text inside the start symbol /* and end symbol */

    Single Line Comment

    Syntax:

    Output

    This is the new paragraph.

    Multi-Line Comment

    Syntax:

    Output

    This is the paragraph to show multiline comment.

    Answered on February 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.