控制语句

  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. [NOIP提高组2018day2t1]旅行

    题目描述 给定n个城市,m条双向道路的图, 不存在两条连接同一对城市的道路,也不存在一条连接一个城市和它本身的道路.并且, 从任意一个城市出发,通过这些道路都可以到达任意一个其他城市.小 Y 只能通过 ...

  2. 全面深入介绍C++字符串:string类

    http://blog.csdn.net/liuliming3000/article/details/1809385 1 从C到C++ string类 2 string类的构造函数 3 string类 ...

  3. SDL_BlitSurface

    SDL_BlitSurface Use this function to perform a fast surface copy to a destination surface. Contents ...

  4. C# 数组&集合&泛型集合

    一.数组 必须规定类型,必须规定长度:  1.定义 int[ ] i = new int[5]; int[] j = new int[]{1,2,3,4,5}; 2.数组的遍历: Console.Wr ...

  5. CF1080

    emmmm......ouuan大佬上紫了,我却没打...... 首先吐槽一波家长会和机房锁门,害我只能来打虚拟赛. 写了abcd四题,还是被ouuan大佬吊打....... 264名,应该能上分吧. ...

  6. 【CF1076D】Edge Deletion 最短路+贪心

    题目大意:给定 N 个点 M 条边的无向简单联通图,留下最多 K 条边,求剩下的点里面从 1 号顶点到其余各点最短路大小等于原先最短路大小的点最多怎么构造. 题解:我们可以在第一次跑 dij 时直接采 ...

  7. java基础题整理(1)

    1.使用length属性获取数组长度,使用length()获取字符串的长度: 2.public.private.protected.friendly区别 public表明该数据成员.成员函数是对所有用 ...

  8. python数据类型(二)

    跟着慕课网练习的,一些简单的知识点如下

  9. MATLAB:图像减法运算(imsubtract函数)

    图像减法运行涉及到imsubtract函数 实现代码如下: clear all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量 clc close all; A=imread('ca ...

  10. CentOS/Linux下设置IP地址

    CentOS/Linux下设置IP地址 1:临时修改:1.1:修改IP地址# ifconfig eth0 192.168.100.100 1.2:修改网关地址# route add default g ...