JavaScript是一种弱类型或者说动态语言。这意味着不用提前声明变量的类型,在程序运行的过程中,类型会被自动确定,并且变量的数据类型会根据赋值的不同发生变化
简单数据类型(基本数据类型)
一,数字型Number
1.JS数字类型既可以保存整数值,也可以保存小数(浮点数)
2. 数字型进制:
- 八进制:数字范围0~7 数字前面加0表示八进制 如010表示8,012表示10
- 十六进制:数字范围0~9 a~f 数字前面加0x表示16进制 如0xa表示10,0xf表示15,0x16表示22
3. 数字型范围:
- 最大值:Number.MAX_VALUE
alert(Number.MAX_VALUE); //1.7976931348623157e+308`
最小值:Number.MIN_VALUE
alert(Number.MIN_VALUE);//5e-324
## 4.数字型三个特殊值:
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117191209253.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
## 5.isNaN
isNaN() 用来判断非数字,并且返回一个值,如果是数字返回false,如果不是数字返回ture
console.log(isNaN(12)); //false
console.log(isNaN('a'));//turn
注意代码中输入字母时加单引号
# 二,字符串型String
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117192333725.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
## 1.字符串引号嵌套
核心:外双内单,外单内双
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117192514885.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
## 2.字符串转义符
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117192643646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
## 3.字符串长度
用变量.length来查询字符串长度
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117192823385.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
var a = 'hahhhaahahah';
alert(a.length); //12
## 4.字符串拼接
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117193214158.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
console.log('我今年' + 18 + '岁'); //我今年18岁
console.log('我今年' + 18); //我今年18
var a = 18;
console.log('我今年a岁') //这样不行
console.log('我今年' + a + '岁'); //我今年18岁
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/2021011719370428.png)
# 三,布尔型Boolean,Undefined和Null
## 1.布尔型
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117194046484.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
## 2.Undefined
如果一个变量声明未赋值 就是undefined 未定义数据类型
undefined和字符串及数字相加效果如下:
var a;
console.log(a + 'my'); //结果为undefinedmy
var b = undefined;
console.log(b + 'my'); //结果为undefinedmy
console.log(b+1); //结果是NaN 一个非数值
## 3.Null
null为空值,null与数字相加等于数字本身
(null返回的是一个空的对象object)
var a = null;
console.log(a); //null
console.log(a + 'my'); //nullmy
console.log(a + 1); //1
console.log(typeof a); //object 对象以后会学到
# 四,获取检查变量数据类型
typeof用来获取检查变量得到数据类型
var a = 10;
console.log(typeof a); //number
var b = 'my';
console.log(typeof b); //string
var c = true;
console.log(typeof c); //boolean
var d = undefined;
console.log(typeof d); //undefined
var e = prompt('请输入')
console.log(typeof b); //string
# 五,数据类型转换
## 1.转换为字符串型
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117200630915.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
## 2.转换为数字型 (重点)
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/2021011720080045.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)
var age = '年龄'
console.log(typeof parseInt(age)); //number
console.log(parseInt('3.6')); //3 取整了
console.log(parseInt('3.12')); //3 取整了
console.log(parseInt('120px')); //120
console.log(parseInt('rem120px')); //NaN
console.log(parseFloat('3.6')); //3.6
console.log(parseFloat('3.12')); //3.12
console.log(parseFloat('3px')); //3
console.log(parseFloat('rem3.12')); //NaN
console.log(Number('rem120px')); //NaN
console.log(Number('120px')); //NaN
console.log(Number('120')); //120
console.log(Number('120.6')); //120.6
//隐式转换
console.log('12'-0); //12
console.log('123'-'120'); //3
console.log('12'*1); //12
## 3.转换为布尔型
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210117202628910.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxOTY5MzMw,size_16,color_FFFFFF,t_70)