1.数据类型和类型转换
(1)数据类型和typeof()
JavaScript中提供了以下几种基本数据类型
String 字符串
Number 数值
Boolean 布尔类型
null 空
undefined 未定义
typeof(dat)可以获取指定数据的数据类型
(2)类型转换
1.转换成数值类型
将数据转换成数值类型有三种固定语法
Number(dat)将dat数据转换成数值
parseInt(dat)将dat数据转换成整数
parseFloatt(dat) 将dat数据转换成浮点数
console.log(Number(5),typeof(Number(5)));
console.log(Number(3.14),typeof(Number(3.14)));
console.log(Number("12"),typeof(Number("12")));
console.log(Number("zhangsan"),typeof(Number("zhangsan")));
console.log(Number("12px"),typeof(Number("12px")));
console.log(Number("a12px"),typeof(Number("a12px")));
console.log(Number(false),typeof(Number(false)));
console.log(Number(true),typeof(Number(true)));
2.转换成字符串
将数据转换成字符串有三种语法
String(dat) 将dat数据转换成字符串数据
dat.toString() 将dat数据转换成字符串形式
dat + “” 使用拼接字符串的形式将dat转换成字符串形式
console.log(String("zhangsan"),typeof(String("zhangsan")));
console.log(String(1223),typeof(String(1223)));
console.log(String(false),typeof(String(false)));
console.log("zhangsan".toString(),typeof("zhangsan".toString()));
console.log((123).toString(),typeof((123).toString()));
console.log((false).toString(),typeof((false).toString()));
console.log("zhangsan"+"",typeof("zhangsan"+""));
console.log(123+"",typeof(123+""));
console.log(false+"",typeof(false+""));
’
3.转换成布尔类型
将数据转换成布尔类型的固定语法为Boolean(dat)
console.log(Boolean(0),typeof(Boolean(0)));
console.log(Boolean(0.0),typeof(Boolean(0.0)));
console.log(Boolean(-20),typeof(Boolean(-20)));
console.log(Boolean(""),typeof(Boolean("")));
console.log(Boolean(" "),typeof(Boolean(" ")));
console.log(Boolean("abc"),typeof(Boolean("abc")));
4.null和undefined转换(坑)
转换成数值类型(注)
console.log(Number(null),typeof(Number(null)));
console.log(parseInt(null),typeof(parseInt(null)));
console.log(parseFloat(null),typeof(parseFloat(null)));
console.log(Number(undefined),typeof(Number(undefined)));
console.log(parseInt(undefined),typeof(parseInt(undefined)));
console.log(parseFloat(undefined),typeof(parseFloat(undefined)));
转换成字符串
console.log(String(null),typeof(String(null)));
console.log(null+"",typeof(null+""));
console.log(String(undefined),typeof(String(undefined)));
console.log(undefined+"",typeof(undefined+""));
console.log((null).toString(),typeof((null).toString())); //此处会报错
console.log((undefined).toString(),typeof((undefined).toString())); //此处会报错
转换成布尔型
console.log(Boolean(null),typeof(Boolean(null)));
console.log(Boolean(undefined),typeof(Boolean(undefined)));
(3)类型隐式转换
某些代码在执行的时候,会在底层自动完成数据类型的转换,这种转换方式为隐式转换
常见的隐式转换有两种
1.isNaN(dat)
作用:判断一个数据是否是一个非数字
console.log(isNaN("123"));
console.log(isNaN("123a"));
2.dat.toFixed(n)
作用:将一个数值类型的数据,指定保留小数位数后转换成字符串
console.log((3.141592654).toFixed(2),typeof((3.141592654).toFixed(2)));
2.运算符
(1)常见的运算符
1.算数运算符
符号 | 描述 | 示例 |
+ | 加法运算符 | 12+9 |
- | 减法运算符 | 12-9 |
* | 乘法运算符 | 12*9 |
/ | 除法运算符 | 12/9 |
% | 取余运算符 | 12%9 |
---、++ | 自减、自增运算符 | a++,a-- |
2.赋值运算符
符号 | 描述 | 示例 |
= | 给变量赋值 | var a = 12 |
-= | 扩展的减法运算 | var a-=12 => var a=a-12 |
+= | 扩展的加法运算 | var a+=12 => var a=a+12 |
*= | 扩展的乘法运算 | var a*=12 => var a =a*12 |
/= | 扩展的除法运算 | var a/ = 12 => var a=a/12 |
3.关系运算符
符号 | 描述 | 示例 |
> | 大于关系 | 18 > 16 结果返回一个布尔值 |
< | 小于关系 | 18<16 |
>= | 大于等于 | 18.<=16 |
<= | 小于等于 | 18 <=16 |
== | 值相等 | 10 = “10” |
!= | 值不相等 | 10 != '11' |
=== | 值与类型都相等【恒等】 | 10 === ‘10’ |
!== | 值或类型不相等 | 10 !== ‘10’ |
4.逻辑运算符
符号 | 描述 | 示例 |
&& | 判断两侧是否都为true(逻辑与) | 10 <12 && 21 <22 |
|| | 判断两侧的条件是否都为false(逻辑或) | 10<12 || 12>>13 |
! | 直接对结果去反(逻辑非) | !(10 > 12) |
(2)运算符的使用
1.算数运算符的使用
var a = 12;
var b = 9;
console.log(a+b,a-b,a*b,a/b,a%b,a++,b--);
注意事项:a++表示先运算再自加,++a表示先自加再运算
var a =10;
var b = 20;
var c = a++ + ++a +b++;
/*
*解析 c= 10 + 12 + b++
* c= 10 + 12 + 20
*/
console.log(a,b,c);
2.比较运算符的使用
console.log(12>18,12<18,12>=18,12<=18,12=="12",12==="12",12!="12",12!=="12");
除了数字,还可以比较字符串:诸位比较字符串的ascii码
console.log("1000">"100","a">"b","ab" > "b","ab">"aaa");
3.逻辑运算符的使用
console.log(10>9&&10>8,10>9&&10<8,10<9&&10<8);
console.log(10>9||10>8,10>9||10<8,10<9||10<8);
console.log(!(false));