条件语句补充,初始引用值,typeof,类型转换

条件语句补充
  • switch case
  • break //终止循环,必须放到循环里面,否则报错
  • continue//终止本循环,继续下一圈循环

switch case

var n = 2;
switch(n) {
	case "a":
		console.log('a');
		break;
	case 2:
		console.log('b');
		break;
	case true:
		console.log('c');
		break;
}
//n和case后面的语句进行比对,找到后会把后面的语句也连带执行出来,所以要加break,终止switch case语句。
//case后面可以是任何数据类型
var date = window.prompt('input');
switch(date) {
	case "星期一":
	case "星期二":
	case "星期三":
	case "星期四":
	case "星期五":
		console.log('上班');
		break;
	case "星期六":
	case "星期天":
		console.log('休息');
		break;
}

break

//终止循环,必须放到循环里面,否则报错
var i = 0;
while(1) {
	i ++;
	console.log(i);
	if (i > 100) {
		break;
	}
}
var i = 0;
var sum = 0;
for (var i = 0; i < 100; i++) {
	sum += i;
	console.log(i);
	if (sum > 100) {
		break;
	}
}

continue

//终止本循环,继续下一圈循环
var i = 0;
for (var i = 0; i < 100; i++) {
	if (i % 7 == 0 || i % 10 == 7) {
		continue;//如果是带7或7的倍数就不打印,继续下一个循环
	}
	console.log(i);

}

初识引用值

  • 数组
  • 对象

数组

//数组,存放数据的仓库
var arr = [1,2,3,"asd",undefined];
console.log(arr[0]);//打印数组的第一位
console.log(arr.length);//打印数组的长度
for (var i = 0; i < arr.length; i++) {
	console.log(arr[i]);//遍历数组中每一位
}
for (var i = 0; i < arr.length; i++) {
	arr[i] = 1;//数组中每一位都变成1
}

var arr = [1,2,3,4,5];
for (var i = 0; i < arr.length; i++) {
	arr[i] += 1;//数组中每一位都加1
}

对象

//对象,存放数据的仓库,属性值可以是任何数据类型
var deng = {
	//属性名 : 属性值,
	lastName : "Deng",
	age : 40,
	sex : undefined,
	wife : "xiaoliu",
	father : "dengdaye",
	son : "xiaodeng",
	handsome : false
}
console.log(deng.lastName);
deng.lastName = "Old Deng";//改变对象中的属性值
console.log(deng.lastName);
编程形式的区别
  1. 面向过程
  2. 面向对象

typeof
六种数据类型
number、string、boolean、undefined、object、function

//typeof返回值的类型,(typeof(num)),(typeof num),括号和空格都可以

var num = 123;
console.log(typeof(num));//number

var num = "123";
console.log(typeof(num));//string

var num = true;
console.log(typeof(num));//boolean

var num = {};//泛泛的引用值,对象
console.log(typeof(num));//object

var num = [];//泛泛的引用值,数组
console.log(typeof(num));//object

var num = null;
console.log(typeof(num));//object

var num = undefined;
console.log(typeof(num));//undefined

var num = function() {};
console.log(typeof(num));//function

//未定义的变量放入typeof里不报错

console.log(typeof(a));//"undefined"返回的是字符串,返回的六种类型都是字符串
console.log(typeof(typeof(a)));//string

类型转换

1.显示类型转换
  • Number(mix)
  • parseInt(string,radix)
  • parseFloat(string)
  • toString(radix)
  • String(mix)
  • Boolean()
//显示类型转换,Number转换为数字类型
var demo = '123';
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : 123

var demo = true;
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : 1

var demo = false;
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : 0

var demo = null;
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : 0

var demo = undefined;
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : NaN

var demo = 'a';
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : NaN

var demo = '-123';
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : -123

var demo = '123abc';
var num = Number(demo);
console.log(typeof(num) + " : " + num);//number : NaN
//parseInt转换为整数,radix基底(多少进制,填2-36)
var demo = '123';
var num = parseInt(demo);
console.log(typeof(num) + " : " + num);//number : 123

var demo = true;
var num = parseInt(demo);
console.log(typeof(num) + " : " + num);//number : NaN

var demo = false;
var num = parseInt(demo);
console.log(typeof(num) + " : " + num);//number : NaN

var demo = '123.9';
var num = parseInt(demo);
console.log(typeof(num) + " : " + num);//number : 123

var demo = 123.9;
var num = parseInt(demo;
console.log(typeof(num) + " : " + num);//number : 123

var demo = 10;
var num = parseInt(demo, 16);//把10当成16进制转换成10进制的数(以目标进制为基底转换成十进制的数)
console.log(typeof(num) + " : " + num);//number : 16

var demo = b;
var num = parseInt(demo, 16);//以目标进制为基底转换成十进制的数
console.log(typeof(num) + " : " + num);//number : 11

var demo = '100px';//以数字位开始看,看到非数字位截止,返回原来的数
var num = parseInt(demo);
console.log(typeof(num) + " : " + num);//number : 100
//parseFloat转换为浮点
var demo = '100.2';
var num = parseFloat(demo);
console.log(typeof(num) + " : " + num);//number : 100.2

var demo = '100.2.3';//以数字位开始看,看到除了第一个点以外的非数字位截止,返回原来的数
var num = parseFloat(demo);
console.log(typeof(num) + " : " + num);//number : 100.2

var demo = '100.2abc';
var num = parseFloat(demo);
console.log(typeof(num) + " : " + num);//number : 100.2
//String转换为字符串
var demo = 123.234;
var num = String(demo);
console.log(typeof(num) + " : " + num);//string : 123.234

var demo = undefined;
var num = String(demo);
console.log(typeof(num) + " : " + num);//string : undefined

var demo = true;
var num = String(demo);
console.log(typeof(num) + " : " + num);//string : true
//Boolean转换为布尔值,
//undefined, null, NaN, "", 0, false ==> 转换为布尔值为false,其他为true
var demo = "";
var num = Boolean(demo);
console.log(typeof(num) + " : " + num);//boolean : false

var demo = "123";
var num = Boolean(demo);
console.log(typeof(num) + " : " + num);//boolean : true
//toString转换为字符串,undefined和null不能用
var demo = 123;
var num = demo.toString();
console.log(typeof(num) + " : " + num);//string : 123

var demo = 10;
var num = demo.toString(8);//以十进制为基底转换为目标进制,把10转换成8进制
console.log(typeof(num) + " : " + num);//string : 12

应用

//parseInt 目标转十进制,toString 十进制转目标进制
//2		----	10     ------     16
var num = 10000;
var test = parseInt(num, 2);
console.log(test.toString(16));//10
类型转换
2.隐式类型转换
  • isNaN()
  • ++/-- +/- (一元正负)
  • +
  • -*/%
  • && || !
  • < > <= >=
  • == !=
//isNaN --> Number();
console.log(isNaN("NaN"));//true
console.log(isNaN(123));//false
console.log(isNaN('123'));//false
console.log(isNaN(null));//false
console.log(isNaN(undefined));//true
console.log(isNaN("abc"));//true

Number('abc')--> NaN//隐式的引用Number进行转换再跟NaN进行比对
//++/--  --> Number();引用Number进行转换 +/-(一元正负) 都会转换为数字类型
var a = "123";
a ++;
console.log(typeof(a) + " : " + a);//number : 124

var a = "abc";
a ++;
console.log(typeof(a) + " : " + a);//number : NaN

var a = +"abc";
console.log(typeof(a) + " : " + a);//number : NaN
//+加号,当加号两边只要有一个是字符串,就把两边都转换为字符串
var a = "a" + 1;
console.log(typeof(a) + " : " + a);//string : a1
//-*/% --> Number();
var a = "1" * 1;
console.log(typeof(a) + " : " + a);//number : 1

var a = "a" * 1;
console.log(typeof(a) + " : " + a);//number : NaN
//&& || ! -->Boolean(); 
//< > <= >=
var a = 1 > "2";//字符串和数字比会转换成数字
console.log(typeof(a) + " : " + a);//boolean : false

var a = "3" > "2";//字符串和字符串比会比较azc码
console.log(typeof(a) + " : " + a);//boolean : true
//== !=
var a = 1 == "1";
console.log(typeof(a) + " : " + a);//boolean : true

var a = 1 == true;
console.log(typeof(a) + " : " + a);//boolean : true

var a = false > true;//转换成数字
console.log(typeof(a) + " : " + a);//boolean : false

var a = 2 > 1 > 3;
console.log(typeof(a) + " : " + a);//boolean : false

var a = 2 > 3 < 1;
console.log(typeof(a) + " : " + a);//boolean : true

var a = 10 > 100 > 0;
console.log(typeof(a) + " : " + a);//boolean : false

var a = 100 > 10 > 0;
console.log(typeof(a) + " : " + a);//boolean : true

var a = undefined > 0;
console.log(typeof(a) + " : " + a);//boolean : false

var a = undefined < 0;
console.log(typeof(a) + " : " + a);//boolean : false

var a = undefined == 0;
console.log(typeof(a) + " : " + a);//boolean : false

var a = null > 0;
console.log(typeof(a) + " : " + a);//boolean : false

var a = null < 0;
console.log(typeof(a) + " : " + a);//boolean : false

var a = null == 0;
console.log(typeof(a) + " : " + a);//boolean : false

var a = undefined == null;
console.log(typeof(a) + " : " + a);//boolean : true

var a = NaN == NaN;//非数不等于任何东西
console.log(typeof(a) + " : " + a);//boolean : false
不发生类型转换
=== !==//绝对等于和绝对不等于
var a = 1 === 1;
console.log(typeof(a) + " : " + a);//boolean : true

var a = 1 === "1";
console.log(typeof(a) + " : " + a);//boolean : false

var a = 1 !== "1";
console.log(typeof(a) + " : " + a);//boolean : true

var a = 1 !== 1;
console.log(typeof(a) + " : " + a);//boolean : false
阅读更多

没有更多推荐了,返回首页