【JavaScript學習筆記】02 JS變量、分支

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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章