一.If...Else 语句
1.If 语句
只有当指定条件为 true 时,该语句才会执行代码。
语法
if (条件)
{
只有当条件为 true 时执行的代码
}
代码
<!DOCTYPE html>
<html>
<body>
<p>如果时间早于 20:00,会获得问候 "Good day"。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
var time = new Date().getHours();
if (time < 20) {
x = "Good day";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
2.If...else 语句
请使用 if....else 语句在条件为 true 时执行代码,在条件为 false 时执行其他代码。
语法
if (条件)
{
当条件为 true 时执行的代码
}
else
{
当条件不为 true 时执行的代码
}
代码
<!DOCTYPE html>
<html>
<body>
<p>点击这个按钮,获得基于时间的问候。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
var time = new Date().getHours();
if (time < 20) {
x = "Good day";
} else {
x = "Good evening";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
3.If...else if...else 语句
使用 if....else if...else 语句来选择多个代码块之一来执行。
语法
if (条件 1)
{
当条件 1 为 true 时执行的代码
}
else if (条件 2)
{
当条件 2 为 true 时执行的代码
}
else
{
当条件 1 和 条件 2 都不为 true 时执行的代码
}
代码
<!DOCTYPE html>
<html>
<body>
<p>点击这个按钮,获得基于时间的问候。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
var time = new Date().getHours();
if (time < 10) {
x = "Good morning";
} else if (time < 20) {
x = "Good day";
} else {
x = "Good evening";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
二.switch 语句
switch 语句用于基于不同的条件来执行不同的动作。
语法
switch(n)
{
case 1:
执行代码块 1
break;
case 2:
执行代码块 2
break;
default:
n 与 case 1 和 case 2 不同时执行的代码
}
工作原理:首先设置表达式 n(通常是一个变量)。随后表达式的值会与结构中的每个 case 的值做比较。如果存在匹配,则与该 case 关联的代码块会被执行。请使用 break 来阻止代码自动地向下一个 case 运行。
代码
<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮来显示今天是周几:</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x;
var d = new Date().getDay();
switch (d) {
case 0:
x = "Today it's Sunday";
break;
case 1:
x = "Today it's Monday";
break;
case 2:
x = "Today it's Tuesday";
break;
case 3:
x = "Today it's Wednesday";
break;
case 4:
x = "Today it's Thursday";
break;
case 5:
x = "Today it's Friday";
break;
case 6:
x = "Today it's Saturday";
break;
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
default 关键词
请使用 default 关键词来规定匹配不存在时做的事情。
代码
<html>
<body>
<p>点击下面的按钮,会显示出基于今日日期的消息:</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x;
var d = new Date().getDay();
switch (d) {
case 6:
x = "Today it's Saturday";
break;
case 0:
x = "Today it's Sunday";
break;
default:
x = "Looking forward to the Weekend";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
三.For 循环
循环可以将代码块执行指定的次数。
语法
for (语句 1; 语句 2; 语句 3)
{
被执行的代码块
}
语句 1 在循环(代码块)开始前执行
语句 2 定义运行循环(代码块)的条件
语句 3 在循环(代码块)已被执行之后执行
代码
<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮,将代码块循环五次:</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
for (var i = 0; i < 5; i++) {
x = x + "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
同时您还可以省略语句 1(比如在循环开始前已经设置了值时)
var i=2,len=cars.length;
for (; i<len; i++)
{
document.write(cars[i] + "<br>");
}
四.While 循环
只要指定条件为 true,循环就可以一直执行代码。
语法
while (条件)
{
需要执行的代码
}
代码
<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮,只要 i 小于 5 就一直循环代码块。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "",
i = 0;
while (i < 5) {
x = x + "The number is " + i + "<br>";
i++;
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
do/while 循环
do/while 循环是 while 循环的变体。该循环会执行一次代码块,在检查条件是否为真之前,然后如果条件为真的话,就会重复这个循环。
语法
do
{
需要执行的代码
}
while (条件);
代码
<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮,只要 i 小于 5 就一直循环代码块。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "",
i = 0;
do {
x = x + "The number is " + i + "<br>";
i++;
}
while (i < 5)
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
当条件不成了时
代码
<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮,只要 i 小于 5 就一直循环代码块。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "",
i = 6;
do {
x = x + "The number is " + i + "<br>";
i++;
}
while (i < 5)
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
五.Break语句
break 语句用于跳出循环。
代码
<!DOCTYPE html>
<html>
<body>
<p>点击按钮,测试带有 break 语句的循环。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "",
i = 0;
for (i = 0; i < 10; i++) {
if (i == 3) {
break;
}
x = x + "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
六.Continue语句
continue 语句中断循环中的迭代,如果出现了指定的条件,然后继续循环中的下一个迭代。
代码
<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮来执行循环,该循环会跳过 i=3 的步进。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "",
i = 0;
for (i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
x = x + "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
效果
七.Throw以及Try 和 Catch语句
throw 语句允许我们创建自定义错误。
正确的技术术语是:创建或抛出异常(exception)。
如果把 throw 与 try 和 catch 一起使用,那么您能够控制程序流,并生成自定义的错误消息。
语法
throw exception
异常可以是 JavaScript 字符串、数字、逻辑值或对象。
try 语句允许我们定义在执行时进行错误测试的代码块。
catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块。
JavaScript 语句 try 和 catch 是成对出现的。
语法
try
{
//在这里运行代码
}
catch(err)
{
//在这里处理错误
}
代码
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction(){
try
{
var x=document.getElementById("demo").value;
if(x=="") throw "值为空";
if(isNaN(x)) throw "不是数字";
if(x>10) throw "太大";
if(x<5) throw "太小";
}
catch(err)
{
var y=document.getElementById("mess");
y.innerHTML="错误:" + err + "。";
}
}
</script>
<h1>我的第一个 JavaScript 程序</h1>
<p>请输入 5 到 10 之间的数字:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">测试输入值</button>
<p id="mess"></p>
</body>
</html>
效果