Home > Web Front-end > JS Tutorial > Several methods of commenting code in javascript (graphic tutorial)

Several methods of commenting code in javascript (graphic tutorial)

亚连
Release: 2018-05-19 13:41:24
Original
3087 people have browsed it

In order to make the program/code easier to read, basically every programming language has the function of commenting, and JavaScript is no exception. There are many forms of JavaScript comment code. This article introduces two methods of commenting code in JavaScript. Friends who need it can refer to

There are generally two methods for javascript comment code:

Single-line comments

Multi-line comments

javascript single-line comments

Single-line comments start with "//" and end at the end of the new line. The following is an example of a javascript single-line comment:

<html>
<head>
<title>javascript单行注释</title>
<script language="javascript">
<!--
// The first alert is below
alert("An alert triggered by JavaScript!");
// Here is the second alert
alert("A second message appears!");
// -->
</script>
</head>
<body>

</body>
</html>
Copy after login

javascript multi-line comment

Multi-line comments start with /*, End with */. The following example uses multi-line comments to explain the code:

<html>
<head>
<title>javascript多行注释</title>
<script language="javascript">
<!--
/*
Below two alert() methods are used to
fire up two message boxes - note how the
second one fires after the OK button on the
first has been clicked
*/
alert("An alert triggered by JavaScript!");
alert("A second message appears!");
// -->
</script>
</head>
<body>

</body>
</html>
Copy after login

How to solve the problem that the browser does not support javascript

App Comment symbols verify whether the browser supports JavaScript script function.

If the user is not sure whether the browser supports JavaScript script, then the comment symbols provided by HTML can be used for verification. HTML comment symbols start with "<--" and end with "-->". If you write a JavaScript script within this comment symbol, browsers that do not support JavaScript will treat the written JavaScript script as a comment.

Use JavaScript script to output a string in the page. Write the JavaScript script in the HTML comment. If the browser supports JavaScript, this string will be output. If not, this string will not be output. The code is as follows:

<html>
<head>
<title>Hide scripts using comments.</title>
<script language="javascript" type="text/javascript">
<!--
document.write("您的浏览器支持JavaScript脚本!");
//-->
</script>
</head>
<body>
Page content here...
</body>
</html>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Basics of the prototype concept of Object objects in JS (detailed content, clear at a glance)

12 entries How to produce high-quality JS code (teaching you to write high-quality code)

Detailed introduction to the difference between Map and ForEach in JS

The above is the detailed content of Several methods of commenting code in javascript (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template