类型转换:
- 运算符和函数会自动将赋予它们的值转换为正确的类型
1、字符串转换
- 将其他类型转换为string, 调用String(变量)函数
- 函数的返回值,重新赋值给原来的变量,改变原变量的数据类型
- alert(value);将value转换为字符串类型,然后显示
字符串转换
let res = true;
console.log(typeof res); //boolean
console.log(res); //true
//将变量作为参数传递给string函数,并将返回值赋值给res
res = String(res);
console.log(res2); //"true"
console.log(typeof res); //string
res = 10;
console.log(typeof res, res); //number 10
res = String(res);
console.log(typeof res, res); //string , "10"
res = null;
console.log(typeof res, res); //Object null
res = String(res);
console.log(typeof res, res); //string, "null"
2、数字转换
- 将其它类型转换为Nubmer类型,调用Number()函数
undefined NaN
null 0
true/false 1/0
string
1、 纯数字字符串,可以转换
2、 数字字符串,前后带空格,可以转换。
3、 数字字符串,中间带空格,不能转换,返回NaN
4、 带字母的字符串等其他,转换为NaN
5、 ""和" " 转换为0
//数字转换
//数字类型字符串
// let str = "123";
// console.log(str, typeof str); //"123" string
// str = Number(str);
// console.log(str, typeof str); //123 number
// //数字字符串 + 前后添加空格
// str = " 123 "
// console.log(str, typeof str); //" 123 " string
// str = Number(str);
// console.log(str, typeof str); //123 number
// //中间带空格
// str = " 1 23 "
// console.log(str, typeof str); //" 1 23 " string
// str = Number(str);
// console.log(str, typeof str); //NaN number
// //带字母
// str = " 123a "
// console.log(str, typeof str); //" 123a " string
// str = Number(str);
// console.log(str, typeof str); //NaN number
// str = "abc"
// console.log(str, typeof str); //"abc" string
// str = Number(str);
// console.log(str, typeof str); //NaN number
// str = ""
// console.log(str, typeof str); //"" string
// str = Number(str);
// console.log(str, typeof str); //0 number
// str = " "
// console.log(str, typeof str); //" " string
// str = Number(str);
// console.log(str, typeof str); //0 number
// str = null;
// console.log(str, typeof str); //null Object
// str = Number(str);
// console.log(str, typeof str); //0 number
// str = undefined;
// console.log(str, typeof str); //undefined undefined
// str = Number(str);
// console.log(str, typeof str); //NaN number
// str = true;
// console.log(str, typeof str); //true boolean
// str = Number(str);
// console.log(str, typeof str); //1 number
// str = false;
// console.log(str, typeof str); //false boolean
// str = Number(str);
// console.log(str, typeof str); //0 number
// let age = Number(prompt("请输入年龄:"));
// console.log(age, typeof age); //20 number
3、布尔转换
-将其他类型转换为boolean类型,调用Boolean()函数
- 直观上为"空"值(比如: 0 空字符串 null undefined NaN)转换为false
- 其他都为true
let temp = 1;
console.log(temp, typeof temp); //1 number
temp = Boolean(temp);
console.log(temp, typeof temp); //true boolean
temp = 0;
console.log(temp, typeof temp); //0 number
temp = Boolean(temp);
console.log(temp, typeof temp); //false boolean
temp = null;
console.log(temp, typeof temp); //null Object
temp = Boolean(temp);
console.log(temp, typeof temp); //false boolean
temp = undefined;
console.log(temp, typeof temp); //undefined undefined
temp = Boolean(temp);
console.log(temp, typeof temp); //false boolean
temp = "abc";
console.log(temp, typeof temp); //abc String
temp = Boolean(temp);
console.log(temp, typeof temp); //true boolean
temp = "";
console.log(temp, typeof temp); //"" string
temp = Boolean(temp);
console.log(temp, typeof temp); //false boolean
temp = " ";
console.log(temp, typeof temp); //"" string
temp = Boolean(temp);
console.log(temp, typeof temp); //true boolean
temp = "0";
console.log(temp, typeof temp); //"0" string
temp = Boolean(temp);
console.log(temp, typeof temp); //true boolean