likes
comments
collection
share

javascript基本数据类型详细解读

作者站长头像
站长
· 阅读数 305

javascript技术栈 - Day01 : 只要不猝死 , 就往死里卷 ~~~javascript基本数据类型详细解读

一、javascript的基本数据类型有哪些

null    
undefined
boolean
string
number
bigint
symbol

二、null基本类型的详细使用

  • 1.null的作用一般是声明一个变量即将绑定一个对象,但是未绑定对象前的一个声明

    let hd;   
    hd = null;        // hd变量即将绑定一个引用类型
    hd = { name : "范冰冰" }
  • 2.a变量是一个引用类型,我们将a变量赋值给b变量,此时我们再将a赋值为null,那么b是null还是引用a的内存地址???
let a = {name : "王百强"}
let b = a;
a = null;   // a ---> null 
console.log(b);    // b ---> {name : "王百强"}
  • 3.null的隐式类型转换
1. 转换为string类型
null + 'hello'   // 'nullhello' 字符串与任何类型相加都是拼接
String(null)// null (切记 : null没有方法)

2. 转换为number类型
123 * null  //  0  (null被转换为number类型的时候就是数值0)
123 + null  // 123
Number(null) // 0
parseInt(null) // NaN (无法计算转换)

3. 转换为boolean类型
Boolean(null) // false
null + true // 1  (null会转换为0,true会转换为1)

4. typeof null   // object

三、undefined基本类型的详情使用

1. 变量声明未赋值
let hd    // undefined
var hd   // undefined

2. 函数中的形参未赋值
function show(a,b){}
show(1)  // b = undefined

3. 函数没有return 返回值
function show(){}
show()   // undefined

4. typeof undefined   // undefined

5. 转换为string类型
let hd
hd + 'hello'    // 'undefinedhello'  (任何值和字符串相加都是相拼接操作)

6. 转换为number类型
let hd
Number(hd)  // NaN (无法转为Number类型)
1 + undefind  // NaN
1 - undefined  // NaN

7. 转换为Boolean类型
Boolean(undefined)   // false
1 + true  // NaN
1 - false  // NaN

四、string基本类型的使用

1、string字面量的创建方法
let str = 'hello world;  
console.log(typeof str)   // string

2、通过构造函数创建字符串类型
let str = new String();     
console.log(typeof str)   // object
str = 'hello'   // string

3、字符串拼接
'hello' + 123/true/null/undefined  //  'hello123' (都是拼接)

4、转number数据类型
'123' - 1   // 122
123 - '2'  // 121
parseInt('123hello')  // 123  
parseInt('hello123') // NaN
parseFloat('123.45hello')   // 123.45
parseFloat('hello123.45')    // NaN
Number('123')   // 123
Number('hello') // NaN

5. 转boolean数据类型
'1' - true  // 0 (都转换为number类型进行计算)
Boolean('')   // 0
Boolean('1') // true
Boolean('hello')  // true

6. string数据类型的自身属性
let str= 'hello world'
str.length;   // 字符串的长度 (自身属性)

7. string数据类型的原型对象中的属性和方法
let str = 'hello'
new str.constructor();  //  创建新的String对象

8. String.prototype.anchor()
- 创建a链接标签 
let str = 'hello'
str.anchor('text');   // <a name="text">str字符串的内容(hello)</a>

9.charAt()
- 通过索引返回字符
let hd = "hello"
hd.charAt(2)  // l
hd.charAt(4)  // o

10.charCodeAt()
- 通过索引返回字符的unicode编码
var str = "HELLO WORLD";
var n = str.charCodeAt(0);     // 72

11.concat()
- 连接多个字符串,返回一个新的字符串,不改变原变量值
let hd = 'hello'
let cms = 'world'
let n = hd + cms;   //  'helloworld'

12.endsWith()
- 当前字符串是否以该字符为结尾
let hd = "hello world"
hd.endWith('world')   // 是的返回true ,否则返回false

13.includes()
- 判断当前字符串中是否包含该字符,返回true/false
let hd = 'houdunren cms .com'
hd.includes('com')  // true

14.indexOf()
- 通过字符返回字符串中第一个匹配到字符的索引位置
- 找到返回字符的索引位置,找不到返回-1
let hd = 'hello';
hd.indexOf('i')  // 0
hd.indexOf('s') // -1

15.lastIndexOf()
- 同indexOf,只不过从后往前检索

16.match()
- 主要用来匹配正则表达式的

17.padStart()
- 前面开始填充
let full = 340102199609111678;
let fullStart = full.slice(-6);   // 截取后6位
let newPad = fullStart.padStart(full.length,'*');  // '*'全部填充到fullStart字符串前面,总共的字符长度为full.length的长度

18.padEnd()
- 向后面进行填充,原理同padStart()

19.repeat()
- 字符串重复的次数
'hello'.repeat(0)   // 0
'abc'.repeat(1)  // abc
'abc'.repeat(2)  // abcabc

20.replace()
- 将字符串中的某些字符全部进行替换,只替换第一次匹配到的字符
let hd = "www.baidu.com"
hd.replace('w',"哈")  // "哈ww.baidu.com" (不改变原字符串,返回新字符串)

21.replaceAll() 
-匹配字符串替换所有的字符串中的字符
let hd  =  "www.baidu.com"
hd.replaceAll('w','1')  // '111.baidu.com'

22.slice()
-截取字符串,不改变原字符串
let hd = "houdunren";
hd.slice(4)   // 从索引4开始截止到尾
hd.slice(1,3)  // 从索引1截取到索引3不包括3
hd.slice(-2)  // 截取最后2个字符
slice(-9,-5)   // 从-9开始截取到-5不包括-5

23.split()
-将字符串按照规则分割成数组,不影响原变量
let hd = "www.baidu.com"
hd.split('.')   // ["www","baidu","com"]

24.startsWith()
- 是否以某个字符串为开头
let hd  = "baddu.com"
hd.startsWith('bad')  // 是的返回true,否则返回false

25.toLowerCase()
- 字符串字母小写
let hd = "HOUDUNREN"
hd.toLowerCase()   // houdunren

26.toUpperCase()
- 字符串字母大写
let hd = 'houdunren'
hd.toUpperCase()  // HOUDUNREN

27.toString()
- 将其他数据类型转换为字符串类型
let hd = true;
hd.toString()   // 'true'

28.trim()
- 去除字符串的前后空白字符
let hd = "   cms    ";
hd.trim()   // 'cms'

29.valueOf()
- 返回当前对象的原始值
let hd = new String('foo')
hd.valueOf()   // 'foo'

30.Symbol(Symbol.iterator)
- 有该属性表示当前的变量对象可以被for...of进行遍历
let cms = "houdunren"
for(leyt key of cms){
      key   //   h  / o / u / d ....
}