转换为字符型

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>转换为字符型</title>
<script>//1.把数字型转换为字符型
var num = 10;
var str = num.toString;

console.log(str);

console.log(typeof str)

//2.我们利用 String(变量)
console.log(String(num));

//3.利用 + 拼接字符串的方法实现转换效果 隐式转换
console.log(num + '我是字符串')</script>
</head>

<body>

</body>

</html>

转换为数字型

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>转换为数字型</title>
<script>//1.parseInt(变量) 可以把字符型转换为数字型 得到的是整数
console.log(parseInt('3.14')); //3 取整
console.log(parseInt('3.94')); //3 取整
console.log(parseInt('120pxAS')); //120 会去掉这个px单位
console.log(parseInt('red3.14PX')); //NaN

//2.parseFloat(变量) 可以把字符型转换为数字型 得到是小数 浮点数
console.log(parseFloat('3.14')); //3.14
console.log(parseFloat('3.94')); //3.94
console.log(parseFloat('120px')); //120 会去掉这个px单位
console.log(parseInt('red3.14PX')); //NaN

//3.利用Number(变量)
var str = '123';
console.log(Number(str)); //数字型

console.log(Number('12')); //数字型

//4.利用算法运算 + - * /
console.log('12' - 0); //12
console.log('123' - '120'); //3
console.log('123' * 1); //123</script>
</head>

<body>

</body>

</html>

转换为布尔型

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>转换为布尔型</title>
<script>.log(Boolean('')); //false
console.log(Boolean(0)); //false
console.log(Boolean(NaN)); //false
console.log(Boolean(null)); //false
console.log(Boolean(undefined)); //false

//除了以上的情况 其余的都是转换成 true
console.log(Boolean('123')); //true
console.log(Boolean('大白'));//true
console.log(Boolean('-------'));//true</script>
</head>

<body>

</body>

</html>