关于javascript:javascript基本数据类型详细解读

12次阅读

共计 3567 个字符,预计需要花费 9 分钟才能阅读完成。

javascript 技术栈 – Day01 : 只有不猝死 , 就往死里卷 ~~~

一、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 ....}



正文完
 0