javascript其他数据类型转换为(number)数值型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
/* //
var v1 = '10';
var v2 = true;
var v3 = false;
var v4 = '';
var v5 = null;
var v6 = undefined;
//方法1: Number(变量名)
console.log(Number(v1)); //10
console.log(Number(v2)); //1
console.log(Number(v3)); //0
console.log(Number(v4)); //0
console.log(Number(v5)); //0
console.log(Number(v6)); //NaN */
/*
// 方法2: 提取整数
var price = '12yen';
var num = 10;
var total = price * 10;
console.log(total);
//NaN 原因是默认转换为Number()转换,price值中包含非数字,所以转换为NaN
// parseInt(变量) 作用:从左向右提取整数,直到遇到第1个非数字结束提取
var price = '12yen';
var price = parseInt(price); //从Price中提取整数
console.log(price); //提取的结果是 12
var num = 10;
var total = price * 10;
console.log(total); //最终的结果是120 */
/*
// 方法3: 提取小数
var price = '12.9';
var num = 10;
// parseFloat(变量) 作用:从左向右提取小数,直到遇到第1个非数字结束提取
var price = parseFloat(price); //从Price中提取小数
console.log(price); //提取的结果是 12.9
var total = price * 10;
console.log(total); //最终结果129
*/
// 基本类型转换为布尔类型
var v1 = 0;
var v2 = '';
var v3 = null;
var v4 = undefined;
var v5 = NaN;
var v6 = '0';
// Boolean(变量) 使用:是将其他类型转换为布尔类型
console.log(Boolean(v1)); //false
console.log(Boolean(v2)); //false
console.log(Boolean(v3)); //false
console.log(Boolean(v4)); //false
console.log(Boolean(v5)); //false
console.log(Boolean(v6)); //true
var 变量1 = 值;
var 变量2 = 值;
var 变量3 = 值;
</script>
</head>
<body>
<H1>只要相的数据才可以直接运算,如果不相同,会根据运算符转换为相同类型</H1>
</body>
</html>