JavaScript学习 - 基础(四) - 控制语句/异常处理
控制语句
if-else 语句
<script>
//控制语句
//if-else格式:
var x = 1
if(x==1){
console.log("this is if")
}else if(x>1){
console.log("this is else if")
}else {
console.log("this else")
}
</script>
switch语句
<script>
//switch语句
var x = 8;
switch (x){
case 1:alert("this 1");break;
case 2:alert("this 2");break;
case 3:alert("this 3");break;
case 4:alert("this 4");break;
case 5:alert("this 5");break;
case 6:alert("this 6");break;
case 7:alert("this 7");break;
default:alert("nothing")
}
</script>
for循环语句
<script>
// for循环:for while
// 基本格式:
for(初始化;条件;增量){语句}
for(i=1;i<100;i++){
document.write(i)
document.write('<br>')
} var x = [1,2,3,4,5,6]
for(i=0;i<x.length;i++){
document.write(x[i])
document.write('<br>')
} //不建议使用
var x = ['a','b','c','d']
for(i in x){
document.write('this i,输出:'+i);
document.write('<br>');
document.write('this x[i],输出:'+x[i]);
document.write('<br>');
document.write('----------------------');
document.write('<br>');
}
// this i,输出:0
// this x[i],输出:a
// ----------------------
// this i,输出:1
// this x[i],输出:b
// ----------------------
// this i,输出:2
// this x[i],输出:c
// ----------------------
// this i,输出:3
// this x[i],输出:d
// ----------------------
<body>
<p>A</p>
<p>B</p>
<p>C</p>
<p>D</p>
<p>E</p>
<p>F</p>
<p>G</p>
</body>
//不建议使用的原因
var x2 = document.getElementsByTagName('p')
for(i in x2) {
document.write('this i,输出:' + i);
document.write('<br>');
document.write('this x[i],输出:' + x2[i]);
document.write('<br>');
document.write('----------------------');
document.write('<br>');
}
// this i,输出:0
// this x[i],输出:[object HTMLParagraphElement]
// ----------------------
// this i,输出:1
// this x[i],输出:[object HTMLParagraphElement]
// ----------------------
// this i,输出:2
// this x[i],输出:[object HTMLParagraphElement]
// ----------------------
// this i,输出:3
// this x[i],输出:[object HTMLParagraphElement]
// ----------------------
// this i,输出:4
// this x[i],输出:[object HTMLParagraphElement]
// ----------------------
// this i,输出:5
// this x[i],输出:[object HTMLParagraphElement]
// ----------------------
// this i,输出:6
// this x[i],输出:[object HTMLParagraphElement]
// ----------------------
// this i,输出:length
// this x[i],输出:7
// ----------------------
// this i,输出:item
// this x[i],输出:function item() { [native code] }
// ----------------------
// this i,输出:namedItem
// this x[i],输出:function namedItem() { [native code] }
</script>
while循环语句
<script>
//while 循环
var i=0;j=0;
while(i<201){
j +=i
i++;
}
document.write(j)
</script>
异常处理
<script>
//异常
//try{主程序}catch(异常){执行}finally{无论是否异常都执行}
//主动抛出异常 throw Error("xxxx")
try{
throw Error("报错了!!!")
document.write("OK")
}
catch (e) {
document.write("异常")
}
finally {
document.write("OK and OK")
}
</script>
JavaScript学习 - 基础(四) - 控制语句/异常处理的更多相关文章
- JavaScript学习基础部分
JavaScript学习基础 一.简介 1.JavaScript 是因特网上最流行的脚本语言,并且可在所有主要的浏览器中运行,比方说 Internet Explorer. Mozilla.Firefo ...
- JavaScript学习记录四
title: JavaScript学习记录四 toc: true date: 2018-09-16 20:31:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...
- javascript学习笔记(四) Number 数字类型
数字格式化方法toFixed().toExponential().toPrecision(),三个方法都四舍五入 toFixed() 方法指定小数位个数 toExponential() 方法 用科学 ...
- Javascript学习笔记四——操作表单
Javascript学习笔记 大多网页比如腾讯,百度云之类的需要登陆,用户输入账号密码就可以登陆,那么浏览器是如何获取用户的输入的呢?今天就记录一下操作表单. 操作表单与操作DOM是差不多的,表单本身 ...
- java基础学习总结四(控制语句<顺序、选择、循环>、方法)
一:结构控制语句 结构控制语句分为3种,顺序语句.选择语句.循环语句. 1:顺序语句 就是自上而下的执行程序,默认执行顺序. 2:选择语句 if结构语句:如果满足条件,则执行该语句. if...els ...
- JavaScript学习总结(四)——jQuery插件开发与发布
jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...
- JavaScript学习 - 基础(二) - 基础类型/类型转换
基础类型 - 数字类型(Number) 1.最基本的数据类型 2.不区分整型数值和浮点型数值 3.所有数字采用64位浮点格式存储,相当于Java和C语言中double格式 4.能表示的最大值 +- 1 ...
- JavaScript学习笔记(四)——jQuery插件开发与发布
jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...
- JavaScript学习基础篇【第1篇】: JavaScript 入门
JavaScript 快速入门 JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中,由<script>...< ...
随机推荐
- LAMP平台部署
LAMP平台的概述 LAMP环境脚本部署:https://github.com/spdir/ShellScripts/tree/master/lamp LAMP的介绍:百度百科 LAMP平台的构成组件 ...
- BZOJ3523[Poi2014]Bricks——贪心+堆
题目描述 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合法的方案或判断 ...
- MT【210】四点共圆+角平分线
(2018全国联赛解答最后一题)在平面直角坐标系$xOy$中,设$AB$是抛物线$y^2=4x$的过点$F(1,0)$的弦,$\Delta{AOB}$的外接圆交抛物线于点$P$(不同于点$A,O,B$ ...
- 自学Linux Shell16.1-函数概念
点击返回 自学Linux命令行与Shell脚本之路 16.1-函数概念 编写比较复杂的shell脚本时,完成具体任务的代码可能需要重复使用.bash shell提供满足这种要求的特性.函数是被赋予名称 ...
- 【BZOJ4555】求和(多种解法混合版本)
[BZOJ4555]求和(多种解法混合版本) 题面 BZOJ 给定\(n\),求 \[f(n)=\sum_{i=0}^{n}\sum_{j=0}^{i}S(i,j)\times 2^j \times ...
- cf1063B Labyrinth (bfs)
可以证明,如果我搜索的话,一个点最多只有两个最优状态:向左剩余步数最大时和向右剩余步数最大时 然后判一判,bfs就好了 dfs会T惨... #include<bits/stdc++.h> ...
- hdu 2845 Beans(最大不连续子序列和)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- Luogu 1351 NOIP 2014 联合权值(贪心,计数原理)
Luogu 1351 NOIP 2014 联合权值(贪心,计数原理) Description 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi, ...
- C# 随机四位数验证码
string str ="abcdefghigklmnopqrstuvwxyzABCDEFJHIGKLMNOPQRSTUVWXYZ1234567890"; while(true){ ...
- (转)JVM——Java虚拟机架构
背景:最近开始忙着找工作了,把需要储备的知识再整理总结一遍!关于JVM的总结,是转自下面的连接.结合<深入理解java虚拟机>,看起来有更清晰的认识. 转载自:http://blog.cs ...