注释用于防止执行语句。编译器执行代码时, 注释将被忽略。注释是用户友好的, 因为用户可以使用注释获得代码的解释。
语法如下:
// For single line comment
/* For block of lines comment......*/
返回值:在执行代码期间, 注释将被忽略。
范例1:此示例说明了使用//的单行注释。
<
!DOCTYPE html>
<
html >
<
head >
<
title >
JavaScript Comments
<
/ title >
<
script >
// Function to add two numbers
function add() {// Declare three variables
var x, y, z;
// Input a number and store it into variable x
x = Number( document.getElementById("num1").value );
// Input a number and store it into variable x
y = Number( document.getElementById("num2").value );
// Sum of two numbers
z= x +y;
// Return the sum
document.getElementById("sum").value = https://www.lsbin.com/z;
}
<
/ script >
<
/ head >
<
body >
Enter the First number: <
input id ="num1" >
<
br >
<
br >
Enter the Second number: <
input id = "num2" >
<
br >
<
br >
<
button onclick = "add()" >
Sum
<
/ button >
<
input id = "sum" >
<
/ body >
<
/ html >
输出如下:
在单击按钮之前:
文章图片
【JavaScript基本语法指南(注释用法实例)】单击按钮后:
文章图片
范例2:此示例说明了使用/ *…* /的多行注释
<
!DOCTYPE html>
<
html >
<
head >
<
title >
JavaScript Comments
<
/ title >
<
script >
/* Script to get two input from user
and add them */
function add() {/* Declare three variable */
var x, y, z;
/* Input the two nos. num1, num2
Input num1 and store it in x
Input num2 and store it in y
The sum is stored in a variable z*/
x = Number(document.getElementById("num1").value);
y = Number(document.getElementById("num2").value);
z = x + y;
document.getElementById("sum").value = https://www.lsbin.com/z;
}
<
/ script >
<
/ head >
<
body >
Enter the First number : <
input id ="num1" >
<
br >
<
br >
Enter the Second number: <
input id = "num2" >
<
br >
<
br >
<
button onclick = "add()" >
Sum
<
/ button >
<
input id = "sum" >
<
/ body >
<
/ html >
输出如下:
在单击按钮之前:
文章图片
单击按钮后:
文章图片
范例3:此示例说明注释的代码将永远不会执行。
<
!DOCTYPE html>
<
html >
<
head >
<
title >
JavaScript Comments
<
/ title >
<
script >
function add() {var 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 = https://www.lsbin.com/z;
}
<
/ script >
<
/ head >
<
body >
Enter the First number: <
input id ="num1" >
<
br >
<
br >
Enter the Second number: <
input id = "num2" >
<
br >
<
br >
<
button onclick = "add()" >
Sum
<
/ button >
<
input id = "sum" >
<
/ body >
<
/ html >
输出如下:
在单击按钮之前:
文章图片
单击按钮后:
文章图片
推荐阅读
- PHP date_default_timezone_get()函数用法介绍
- U盘做系统,本文教您U盘做系统办法
- 笔记本u盘打开,本文教您联想笔记本u盘打开的办法
- u盘乱码,本文教您u盘出现乱码的处理办法
- u盘不显示怎样办,本文教您处理u盘不显示的办法
- u盘无法弹出,本文教您u盘无法弹出怎样办
- ripper病毒,本文教您u盘中了ripper病毒怎样办
- u盘读写速度,本文教您u盘读写速度多少是正常的
- bios设置u盘打开,本文教您神州笔记本bios设置u盘打开的办法