2018.4.20
变量
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>定义变量</title>
</head>
<body>
<script type="text/javascript">
/*
定义变量
这里只是定义了一个变量,var是一个关键字,只是声明这是一个变量
*/
var i =true;
//var i ="我";
//var i =null;//
console.log(typeof i);//[Web浏览器] "number" /day30/02-JS-variable.html (14)
// [Web浏览器] "boolean" /day30/02-JS-variable.html (14)
// [Web浏览器] "object" /day30/02-JS-variable.html (16)
var s;
console.log(typeof s);//undefined 在定义是没有赋予初值,无法确定数据类型、
n = null;//这里可以这样定义变量,但是有一些限制
console.log(typeof n);
function varTest() {
/*
在方法里面声明变量使用var关键字,那么这个变量是一个局部变量,只能在函数的大括号里面
范围以内使用
如果没有用var关键字声明变量的话,这个变量就是一个【全局】变量。
*/
m =10;
// var t = 20;
console.log("--->"+i);
console.log("--->"+n);
}
varTest();
console.log("--->"+m);
// console.log("---->"+t);
</script>
</body>
</html>
结果:
[Web浏览器] "boolean" /day30/02-JS-variable.html (16)
[Web浏览器] "undefined" /day30/02-JS-variable.html (22)
[Web浏览器] "object" /day30/02-JS-variable.html (24)
[Web浏览器] "--->true" /day30/02-JS-variable.html (34)
[Web浏览器] "--->null" /day30/02-JS-variable.html (35)
[Web浏览器] "--->10" /day30/02-JS-variable.html (40)
分支 if switch case while
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分支循环</title>
</head>
<body>
<script type="text/javascript">
var i = 1;
/*
这种写法的switch-case 支持java c c++ javascript php python perl oc swift(和js很像)
*/
switch (i){
case 1:
console.log("这里是switch-case1")
break;
case 2:
console.log("这里是switch-case1")
break;
default:
console.log("搞事情");
break;
}
/*
在javascrpt当中,for循环的大括号不能作为【变量作用域】的约束
只有javascript里面方法大括号才可以作为【变量作用域的约束】
*/
// for (var j = 0; i < 10; j++) {
// console.log("---->"+j);
// }
// console.log("???"+ );
//
var k = 10;
while(k-- > 0) {
if(0 == k% 2) {
console.log("jS就是这么灵活")
}else{
console.log("xa ")
}
}
</script>
</body>
</html>
结果:
[Web浏览器] "这里是switch-case1" /day30/03-js-switch-loop.html (15)
[Web浏览器] "xa " /day30/03-js-switch-loop.html (40)
[Web浏览器] "jS就是这么灵活" /day30/03-js-switch-loop.html (38)
[Web浏览器] "xa " /day30/03-js-switch-loop.html (40)
[Web浏览器] "jS就是这么灵活" /day30/03-js-switch-loop.html (38)
[Web浏览器] "xa " /day30/03-js-switch-loop.html (40)
[Web浏览器] "jS就是这么灵活" /day30/03-js-switch-loop.html (38)
[Web浏览器] "xa " /day30/03-js-switch-loop.html (40)
[Web浏览器] "jS就是这么灵活" /day30/03-js-switch-loop.html (38)
[Web浏览器] "xa " /day30/03-js-switch-loop.html (40)
[Web浏览器] "jS就是这么灵活" /day30/03-js-switch-loop.html (38)