'What is the difference between the <!-- text --> and /* text */ comments?

What is the difference between the <!-- text --> and /* text */ comments? Are they both the same thing or is there a difference in function?



Solution 1:[1]

Yes, they're almost the same thing. However, we use <!--text--> in HTML and use /*text*/ in CSS, JavaScript and many others.

Solution 2:[2]

/* comments are used in C like languages, including Java, JavaScript, and CSS.

<!-- comments are used in SGML / HTML / XML like languages.

If the language you are considering uses one or both, as long as the language recognizes it as a comment, there's probably not a difference.

The only reason to choose one or the other when designing a language is to for consistency within the language or to avoid syntax constructs that would be better used for something else.

Solution 3:[3]

Same thing just depends on the language you are writing code in. Use a code editor like VS code to automatically write comments in whatever detected language by a simple shortcut instead of memorizing.

Solution 4:[4]

All the above answers are correct.

As you mentioned the HTML tag in the question so both types of comments syntax can be used for single-line commenting and multi-line commenting but we have to know where we have to use which type of commenting syntax.

<!DOCTYPE html>
<html>
    <head>
        <title>Hari's demo page</title>
        <style>
            h1 {
                color: blueviolet; /* set h1 color to bv 
                set h2 color to red
                set h3 color to green 
                */
            }
            h2 {    color:chocolate; /* set h2 to chocolate */  }
            h3 {    color: green; /* set h3 to green */  }
        </style>
    </head>
    <body>
        <!--define h1
        define h2
        define h3-->
        <h1>Heading 1</h1>
        <h2>Heading 2</h2>
        <h3>Heading 3</h3> /* defined h3 columns */
    </body>
</html>

I have used <!--Content--> type to single-line as well as multi-line in HTML as you can see in the below image how it is displayed/rendered in the browser.

/* Content */ is used mostly in CSS, Java, C#, etc., as you can see same in the below image how I included these types of comments in the CSS styling part as well as in the body part (in HTML) and how it is rendered in the browser:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cup11
Solution 2 U. Windl
Solution 3 eniigma
Solution 4