控制语句

  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学习 - 基础(四) - 控制语句/异常处理的更多相关文章

  1. JavaScript学习基础部分

    JavaScript学习基础 一.简介 1.JavaScript 是因特网上最流行的脚本语言,并且可在所有主要的浏览器中运行,比方说 Internet Explorer. Mozilla.Firefo ...

  2. JavaScript学习记录四

    title: JavaScript学习记录四 toc: true date: 2018-09-16 20:31:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  3. javascript学习笔记(四) Number 数字类型

    数字格式化方法toFixed().toExponential().toPrecision(),三个方法都四舍五入 toFixed() 方法指定小数位个数  toExponential() 方法 用科学 ...

  4. Javascript学习笔记四——操作表单

    Javascript学习笔记 大多网页比如腾讯,百度云之类的需要登陆,用户输入账号密码就可以登陆,那么浏览器是如何获取用户的输入的呢?今天就记录一下操作表单. 操作表单与操作DOM是差不多的,表单本身 ...

  5. java基础学习总结四(控制语句<顺序、选择、循环>、方法)

    一:结构控制语句 结构控制语句分为3种,顺序语句.选择语句.循环语句. 1:顺序语句 就是自上而下的执行程序,默认执行顺序. 2:选择语句 if结构语句:如果满足条件,则执行该语句. if...els ...

  6. JavaScript学习总结(四)——jQuery插件开发与发布

    jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...

  7. JavaScript学习 - 基础(二) - 基础类型/类型转换

    基础类型 - 数字类型(Number) 1.最基本的数据类型 2.不区分整型数值和浮点型数值 3.所有数字采用64位浮点格式存储,相当于Java和C语言中double格式 4.能表示的最大值 +- 1 ...

  8. JavaScript学习笔记(四)——jQuery插件开发与发布

    jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...

  9. JavaScript学习基础篇【第1篇】: JavaScript 入门

    JavaScript 快速入门 JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中,由<script>...< ...

随机推荐

  1. BZOJ1798[Ahoi2009]维护序列——线段树

    题目描述     老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成.    有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2 ...

  2. JavaScript——根据数组中的某个值进行排序

    我这里是根据次数进行倒叙,可根据自己情况进行修改 function sortKey(array,key){ return array.sort(function(a,b){ var x = a[key ...

  3. .net 手机滑动加载

    $(window).scroll(function () { var scrollTop = $(this).scrollTop(); var scrollHeight = $(document).h ...

  4. UOJ33 [UR #2] 树上GCD 【点分治】【容斥原理】【分块】

    题目分析: 树上点对问题首先想到点分治.假设我们进行了点分治并递归地解决了子问题.现在我们合并问题. 我们需要找到所有经过当前重心$ c $的子树路径.第一种情况是LCA为当前重心$ c $.考虑以$ ...

  5. IDEA在debug时修改变量值

    IDEA在debug调试时修改变量值 例如以下代码: int y1 = 0; anchor.setDy1(y1); 在代码中,这个y1永远是0,但是y1本身是个变量 debug的时候获取到这个属性,并 ...

  6. LeetCode好题汇总

    最近开始刷LeetCode,准备按照专题来进行.所有的解题方案我都会放在GitHub上面,对于有价值的题目,我会重新在这里做记录,并且将解题方案贴出来,便于自己之后复习. Array 1. easy ...

  7. CF1101D GCD Counting(数学,树的直径)

    几个月的坑终于补了…… 题目链接:CF原网  洛谷 题目大意:一棵 $n$ 个点的树,每个点有点权 $a_i$.一条路径的长度定义为该路径经过的点数.一条路径的权值定义为该路径经过所有点的点权的 GC ...

  8. 【uoj291】 ZJOI2017—树状数组

    http://uoj.ac/problem/291 (题目链接) 题意 一个写错的树状数组有多大的概率与正常树状数组得出的答案一样. Solution 可以发现这个树状数组维护的是后缀和. 所以二维线 ...

  9. Spring的后置处理器BeanFactoryPostProcessor

    新建一个JavaBean UserBeanFactoryPostProcessor 实现了BeanFactoryPostProcessor接口 Spring配置文件如下: 编写测试用例 从结果可以看出 ...

  10. JavaScript窗体Window.ShowModalDialog使用详解

    Javascript有许多内建的方法来产生对话框,如:window.alert(), window.confirm(),window.prompt().等. 然而IE提供更多的方法支持对话框.如: s ...