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

Several methods of commenting code in javascript (graphic tutorial)

亚连
亚连Original
2018-05-19 13:41:243119browse

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>

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>

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 "f98a2fb10194037464bfe9c316f9d8ac". 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>

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!

Statement:
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