一. 数据类型
JavaScript是一门弱类型的语言,和Java的强类型不同,js在声明一个变量的时候,不需要声明类型,JS无论声明什么类型的变量都统一使用var来声明,具体的类型需要给变量赋值后才能获得,且赋值可以动态转换
-
string:字符串
-
number:数值
-
boolean:布尔
-
undefined:未定义的
-
object:对象类型
下面针对上面的几种数据类型分别举例,string:字符串
<script type="text/javascript"> var a='s'; console.log(typeof a); //类型是String </script>
利用typeof可以输出a的类型,再看一下number:数值
<script type="text/javascript"> var a = 10; console.log(typeof a) //类型是number a = 2.5; console.log(typeof a) //类型是number </script>
boolean:布尔
<script type="text/javascript"> var a = true; console.log(typeof a) //类型是boolean </script>
undefined:未定义的
<script type="text/javascript"> var a; console.log(typeof a) //类型是undefined </script>
object:对象类型
<script type="text/javascript"> var a = null; console.log(typeof a) //类型是Object a = {}; //js对象 console.log(typeof a) //类型是Object a = [];//js数组 console.log(typeof a) //类型是Object </script>
js的number类型计算有时候会丢失精度:
<script type="text/javascript"> var a = 0.1; var b = 0.3; console.log(b + a); // 0.4 console.log(b - a); // 0.19999999999999998 </script>
字符串和数字的加法和减法规则是不同的,首先来看字符串和数字的加法:
<script type="text/javascript"> x = 1 + 2; document.write(x); //3 document.write("<br />"); x = "1" + "2"; document.write(x); //12 document.write("<br />"); x = 1 + "2"; document.write(x); //12 document.write("<br />"); x = "1" + 2; document.write(x); //12 document.write("<br />"); </script>
可以看到,字符串+数字,就是做了一个简单的拼接。再来看一下字符串和数字的减法:
<script type="text/javascript"> x = 6 - 1; document.write(x); //5 document.write("<br />"); x = "6" - "1"; document.write(x); //5 document.write("<br />"); x = 6 - "1"; document.write(x); //5 document.write("<br />"); x = "6" - 1; document.write(x); //5 document.write("<br />"); </script>
可以看到,无论被减数和减数是数字还是字符串,都会进行计算。
二. 类型转换
1. 其他类型转换为String
有两种方法,第一种:使用toString方法
<script type="text/javascript"> var a = 10; a.toString(); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); a = a.toString(); document.write(typeof a + "——> a = " + a); //string——> a = 10 document.write("<br />"); a = true; a = a.toString(); document.write(typeof a + "——> a = " + a); //string——> a = true document.write("<br />"); a = false; a = a.toString(); document.write(typeof a + "——> a = " + a); //string——> a = false document.write("<br />"); /* a = undefined; a = a.toString(); document.write(typeof a + "——> a = " + a); //报错 document.write("<br />"); a = null; a = a.toString(); document.write(typeof a + "——> a = " + a); //报错 document.write("<br />");*/ </script>
第二种,使用String()方法
<script type="text/javascript"> var a = 10; String(a); document.write(typeof a + "——> a = " + a);; //number——> a = 10 document.write("<br />"); a = String(a); document.write(typeof a + "——> a = " + a); //string——> a = 10 document.write("<br />"); a = true; a = String(a); document.write(typeof a + "——> a = " + a); //string——> a = true document.write("<br />"); a = false; a = String(a); document.write(typeof a + "——> a = " + a); //string——> a = false document.write("<br />"); a = undefined; a = String(a); document.write(typeof a + "——> a = " + a); //string——> a = undefined document.write("<br />"); a = null; a = String(a); document.write(typeof a + "——> a = " + a); //string——> a = null document.write("<br />"); </script>
可以看到,使用String方法后,undefined和null类型不再报错
2. 其他类型转换为number
<script type="text/javascript"> var a = "10"; Number(a); document.write(typeof a + "——> a = " + a); //string——> a = 10 document.write("<br />"); a = Number(a); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); a = true; a = Number(a); document.write(typeof a + "——> a = " + a); //number——> a = 1 document.write("<br />"); a = false; a = Number(a); document.write(typeof a + "——> a = " + a); //number——> a = 0 document.write("<br />"); a = undefined; a = Number(a); document.write(typeof a + "——> a = " + a); //number——> a = NaN document.write("<br />"); a = null; a = Number(a); document.write(typeof a + "——> a = " + a); //number——> a = 0 document.write("<br />"); document.write("------------------------------"); document.write("<br />"); a = "10.5a"; a = Number(a); document.write(typeof a + "——> a = " + a); //number——> a = NaN document.write("<br />"); document.write("------------------------------"); document.write("<br />"); a = "10"; a = parseInt(a); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); a = "10.5"; a = parseInt(a); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); a = "10a"; a = parseInt(a); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); a = "10.5a"; a = parseInt(a); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); document.write("------------------------------"); document.write("<br />"); a = "10"; a = parseFloat(a); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); a = "10.5"; a = parseFloat(a); document.write(typeof a + "——> a = " + a); //number——> a = 10.5 document.write("<br />"); a = "10a"; a = parseFloat(a); document.write(typeof a + "——> a = " + a); //number——> a = 10 document.write("<br />"); a = "10.5a"; a = parseFloat(a); document.write(typeof a + "——> a = " + a); //number——> a = 10.5 document.write("<br />"); a = "10.5.2"; a = parseFloat(a); document.write(typeof a + "——> a = " + a); //number——> a = 10.5 document.write("<br />"); /* string——> a = 10 number——> a = 10 number——> a = 1 number——> a = 0 number——> a = NaN number——> a = 0 ------------------------------ number——> a = NaN ------------------------------ number——> a = 10 number——> a = 10 number——> a = 10 number——> a = 10 ------------------------------ number——> a = 10 number——> a = 10.5 number——> a = 10 number——> a = 10.5 number——> a = 10.5*/ </script>
3. 其他类型转换为boolean
<script type="text/javascript"> var a = "111"; Boolean(a); document.write(typeof a + "——> a = " + a); //string——> a = 111 document.write("<br />"); a = "111"; a = Boolean(a); document.write(typeof a + "——> a = " + a); //boolean——> a = true document.write("<br />"); a = -1.1; //只要是非0的数字都是true,比如1和-1, -1.1和1.1都是true a = Boolean(a); document.write(typeof a + "——> a = " + a); //boolean——> a = true document.write("<br />"); document.write("------------------------------"); document.write("<br />"); a = 0; a = Boolean(a); document.write(typeof a + "——> a = " + a); //boolean——> a = false document.write("<br />"); a = ""; a = Boolean(a); document.write(typeof a + "——> a = " + a); //boolean——> a = false document.write("<br />"); a = undefined; a = Boolean(a); document.write(typeof a + "——> a = " + a); //boolean——> a = false document.write("<br />"); a = null; a = Boolean(a); document.write(typeof a + "——> a = " + a); //boolean——> a = false document.write("<br />"); a = NaN; //NaN本身是number类型 a = Boolean(a); document.write(typeof a + "——> a = " + a); //boolean——> a = false document.write("<br />"); </script>
请登录之后再进行评论