控制语句

  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. BZOJ1500[NOI2005]维修数列——非旋转treap

    题目描述 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 输入 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初 ...

  2. MT【36】反函数有关的一道题

    解答:$\frac{7}{2}$ 做适当的变换,再令$x-1=t$容易划归到我们熟悉的题型,$2^t=\frac{3}{2}-t,log_2t=\frac{3}{2}-t$作图或者利用函数单调性可得$ ...

  3. 自学Linux Shell18.1-sed编辑器基础特性

    点击返回 自学Linux命令行与Shell脚本之路 18.1-sed编辑器基础特性 linux世界中最广泛使用的两个命令行编辑器: sed gawk 1. sed概念 sed是stream edito ...

  4. [POI2010]KLO-Blocks——一道值得思考的题

    题目大意: 给出N个正整数a[1..N],再给出一个正整数k,现在可以进行如下操作:每次选择一个大于k的正整数a[i],将a[i]减去1,选择a[i-1]或a[i+1]中的一个加上1.经过一定次数的操 ...

  5. JAVA类中获取项目路径

    在java web项目中获取项目的src/main/resource下的文件路径 当前类名.class.getClassLoader().getResource("/").getP ...

  6. 【洛谷P1638】逛画展

    题目大意:给定 N 个数字组成的序列,求刚好拥有所有 M 种数字的最短区间. 题解:双指针算法是一种对于暴力的优化算法,对于这道题来说,一个显然的暴力是:对于序列中每一个位置 pos,计算出这个位置右 ...

  7. springboot下实现邮件发送功能

    springboot给我们封装好了邮件功能,非常简单,只需要稍微配置下就ok. 引入jar <dependency> <groupId>org.springframework. ...

  8. tomcat中配置servlet.xml的JNDI或JDBC连接数据库【原】

    tomcat中配置servlet.xml的JNDI或JDBC连接数据库 一. JNDI 1. tomcat环境 找到X:\xxx\......\apache-tomcat-6.0.39\conf\se ...

  9. BigDecimal最基础用法【转】

    BigDecimal最基础用法 用字符串生成的BigDecimal是不会丢精度的. 简单除法. public class DemoBigDecimal { public static void mai ...

  10. 线程本地变量ThreadLocal (耗时工具)【原】

    线程本地变量类 package king; import java.util.ArrayList; import java.util.List; import java.util.Map; impor ...