使用typeof 查看
01 基本数据类型
1. number (整数型 浮点型)
var a = 10.12 ;console.log(a)
2. string (字符串)
var a = 'hello' ;console.log(a)
3.boolen (true/false)
var a = ture ;var b = false;console.log(a)console.log(b)
02 引用数据类型
1.array (数组)
//1. 数组:array var ae = ["html",1,true ] //2.获取数组的值 console.log(ae[0]) console.log(ae[1]) console.log(ae[2]) // 3.获取数组的长度 console.log(ae.length) // 4.获取数组最后一位 console.log(ae[ae.length-1]) // 5.数组后添加元素,可以添加多个 ae.push('css','js'); console.log(ae) // 6.数组前添加元素 ae.unshift(0,11) console.log(ae) // 7.删除数组第一个元素 ae.shift() console.log(ae)
2.object (对象)
//json对象:键值对的形式 // key:value var chen ={ 'name':'cheng', 'age': '18', 'sex':'male' } console.log(chen.age)
3.function (函数)
//函数:封装特定功能的代码块 function go(){ console.log('hello world') } // 函数只有调用才能执行 go()
03 基本数据类型转换
3-1 to string
//String() //toString()//+""利用+的拼接特性var a = 18.25, b = true;console.log(String(a));console.log(String(b));
3-2 to number
/* string-->number 用Number()转换成数值型,tips:只能识别纯数字的字符串,其他字符输出NANparseInt()转换成整数型parseFloat()准换成浮点型boolean-->numbertrue-->1false-->0*/var str = "3.14"console.log(Number(str))console.log(Number("3px"))//NaN 非数字console.log(Number("number 3"))//NaN 非数字Number(Null)//0Number(undefined)//NaN
3-3 to boolean
/* string,number-->booleanBoolean()string中除了紧邻""为false 其余为true number除了0为false,其余为true*/var str = "sda",s = '',f = ' ';console.log(Boolean(str));//trueconsole.log(Boolean(s));//falseconsole.log(Boolean(f));//truevar num1 = 0 ,num2 = 0.5, num3=1, num4 = -1;console.log(Boolean(num1))console.log(Boolean(num2))console.log(Boolean(num3))console.log(Boolean(num4))
3-4 练习
console.log(parseInt(""))//NaN console.log(parseFloat(""))//NaN console.log(Number(""))//0 console.log(10>"")//true console.log(0>"sd")//false
console.log(10 && 5)//5 console.log(0 && 20)//0 console.log(10 && 0)//0 console.log(" " && 20)//20 console.log("aa" && 20)//20