javaScript中數據類型和數組的案例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javaScript中數據類型和數組的案例</title>
</head>
<body>
<h1>javaScript中數據類型和數組的案例</h1>
</body>
<script type="text/javascript">
//案例1
var a = '江西省贛州市於都縣';
console.log(typeof a);
console.log(typeof (a));

a = 666;
console.log(typeof a);
console.log(typeof (a));

function fn1(){
	return 888;
}

a = fn1();
console.log(typeof a);
console.log(typeof (a));

function fn2(){
	console.log('我是fn2()函數');
}

a = fn2();
console.log(typeof a);
console.log(typeof (a));

//
var b = 123;
console.log(typeof b);
console.log(typeof (b));

b = '於都縣';
console.log(typeof b);
console.log(typeof (b));

b = fn3();
function fn3(){
	return 835;
}
console.log(typeof b);
console.log(typeof (b));

b = fn4();
function fn4(){
	console.log('我是fn4函數');
}
console.log(typeof b);
console.log(typeof (b));

var c = 5;
console.log(typeof c);
console.log(typeof (c));

c = null;
console.log(typeof c);
console.log(typeof (c));

c = undefined;
console.log(typeof c);
console.log(typeof (c));

console.log(typeof null);
console.log(typeof (null));

console.log(typeof undefined);
console.log(typeof (undefined));

var d = false;
console.log(typeof d);
console.log(typeof (d));
d = true;
console.log(typeof d);
console.log(typeof (d));
//創建數組的4種語法
//隱式創建(推薦使用)
var names = ['蘇荃', '曾柔', '阿珂', '建寧公主', '雙兒', '沐劍屏', '方怡'];
var result = Array.isArray(names);
console.log(result, names.length);
for ( var name in names) {
	console.log(name);
}

//直接實例化
var citys = new Array('南昌', '贛州', '上饒', '萍鄉');
var result2 = Array.isArray(citys);
console.log(result2, citys.length);
for (var index = 0; index < citys.length; index++) {
	console.log(citys[index]);
}

//創建數組並指定長度
var hobbys = new Array(3);
hobbys[0] = '吃';
hobbys[1] = '喝';
hobbys[2] = '嫖';
var result3 = Array.isArray(hobbys);
console.log(result3, hobbys.length);
for (var i = 0; i < hobbys.length; i++) {
	var hobby = hobbys[i];
	console.log(hobby);
}
//可以再次添加元素,定義的數組大小對此沒有影響
hobbys[3] = '賭';
hobbys[4] = '騙';
result3 = Array.isArray(hobbys);
console.log(result3, hobbys.length);
for (var i = 0; i < hobbys.length; i++) {
	var hobby = hobbys[i];
	console.log(hobby);
}

//創建數組但不指定長度
var arr = new Array();  
arr[0] = 123;
arr[1] = "abc";
arr[5] = false;
console.log(arr[3]);//
console.log(arr);//
console.log(arr[10]);//undefined,不會出現數組越界異常

//for in 遍歷,未定義的不遍歷
for(var i in arr){
	console.log(arr[i]);
}
            
//數組案例
var m = [false, "aaa", 123, 'hello', true, 56.78];
console.log(m);
console.log(m.length);

var n = new Array(false, "hi", 567, 'jack', true, 15.34);
console.log(n);
console.log(n.length);

var h = new Array(6);
h[0] = 'jack';
h[1] = "tom";
h[2] = 1789;
h[3] = false;
h[4] = 55.66;
h[5] = true;
console.log(h);
console.log(h.length);
//可以再次添加元素,定義的數組大小對此沒有影響
h[6] = '於都縣渡江大道';
h[7] = 26;
console.log(h);
console.log(h.length);

var k = new Array();
k[0] = '江西省於都縣';
k[1] = 23456;
k[5] = false;
k[12] = 40.56;
k[16] = true;
console.log(k);//
console.log(k.length);
//
console.log(k[0], k[1], k[2], k[5], k[7], k[16], k[12]);
//不會出現數組越界異常
console.log(k[20]); //
for ( var element in k) {
	console.log(element);
}
console.log('****************');
for (var index = 0; index < k.length; index++) {
	console.log(k[index]);
}
</script>
</html>

 

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