原生js基础之常见数据类型

一.常见数据类型

js常用数据类型:数字类型、字符串、布尔、数组

1. 数字类型(Number)

所有数字对应的类型

1)typeof(数据)

获取指定数据对应的类型

2)数学对象(Math)
  • a.小数转整数
console.log(Math.ceil(5.8))    // 6
     console.log(Math.floor(5.8))   // 5
     console.log(Math.round(5.8), Math.round(5.4))

  • b.常用运算
 // x的y次方
     console.log(Math.pow(3, 2), Math.pow(16, 0.5), Math.pow(8, 1/3))    // 9 4  2
     console.log(2**3)
 // 求绝对值
   console.log(Math.abs(-19))
  • c.随机数
 // 产生0~1随机小数
     console.log(Math.random())  
// 产生0~100随机整数
     console.log(parseInt(Math.random()*100))
 // 产生100~999随机数
     console.log(Math.random()*899+100)

3)类型转换
     // Number() - 转换成数字
     console.log(Number('12.4'))
     // parseFloat() - 将数据转换成带小数点的数字
     // parseInt()  -  数数据转换成整数
     // Math.ceil()/ Math.floor()/Math.round()
     

2.字符串(String)

1)表示方式
 str1 = '小明'
     str2 = "四川成都"
     str3 = `我是${str1},我来自${str2}`
     console.log(str1, str2, str3)
     console.log(typeof(str3))
2)转义字符
 str4 = '\t\'abc\n123'
     console.log(str4)
     str5 = '\u4e00abc'
     console.log(str5)       // 一abc
3)获取字符
  • js中下标值的范围是 0 ~ 长度-1
  • 下标没有-1,越界也不会报错
  • 遍历
 for(let x in message){
         console.log(x, message[x])
     }
4)相关操作操作
  • a.加法
console.log('abc'+'123', 'abc'+89090, 'abc'+true)    //abc123
  • 不支持乘法运算
  • b.比较运算(和python一样)
5)相关方法和属性
  • a.获取字符串长度 . length
  • c.获取字符
  • d.正则相关
    字符串.match(正则表达式) - 查找字符串中能够满足正则的子串
  • e.字符串.replace(正则, 新子串)
  • f.切片
    字符串.slice(开始下标, 结束下标)
6)类型转换

String(数据) - 将数据转换成字符串

3.布尔(Boolean)

只有true和false两个值

4.数组(Array)

1)增删改查
a.查
  • names[下标]
 console.log(names[0])    // 诸葛亮
  • 遍历
names = ['诸葛亮', '赵云', '曹操', '荀彧', '郭嘉', '庞统']
for(let index in names){
        console.log(index, names[index])
    }
  • 数组.forEach(有一个或者两个参数的函数)
b.增
  • 数组.push(元素)
  • 数组.splice(下标,0,元素1,元素2,元素3,…) - 在指定下标前插入指定元素
c.删
  • 数组.pop()
  • 数组.splice(下标, 删除个数) - 从指定下标开始删除指定个数的元素
d.改

数组[下标] = 值

2)相关方法
a.数组.every(函数)
  • 检测数组中所有的元素是否满足函数返回值提供的条件
  • 函数 - 需要一个参数,这个参数指向的是数组中的每个元素
b.数组.filter(函数)
  • 获取数组所有满足函数返回值要求的元素
  • 函数 - 需要一个参数,这个参数指向的是数组中的每个元素
c.数组.join(分隔符=’,’)
  • 将数组中所有的元素适用分隔符拼接成一个字符串
names = ['张小明', '张三', '张三丰', '张无忌', '张翠山']
    result = names.join()
    console.log(result)   // 张小明,张三,张三丰,张无忌,张翠山
    
    result = names.join('')
    console.log(result)    // 张小明张三张三丰张无忌张翠山
    
    scores = [29, 89, 78, 67, 99, 45, 75, 39]
    result = scores.join()
    console.log(result)    // 29,89,78,67,99,45,75,39

d.数组.map(函数)

将原数字中所有的元素按照函数做指定的操作后,产生新的数组

e.数组.reduce(函数, 初始值)
  • 运算规则和python中的reduce一样
// 求所有元素的和
    result = scores.reduce(function(x, item){
        return x+item
    }, 0)
    console.log(result)
    
    // 求所有元素个位数的和
    result = scores.reduce(function(x, item){
        return x+item%10
    }, 0)
    console.log(result)
f.排序
  • 数组.sort(函数)
  • 函数有两个参数,指向的都是序列中的元素
 scores = [29, 89, 78, 67, 99, 45, 75, 39]
    scores.sort(function(a, b){
        // 按照元素的大小从小到大排序
        // return a-b
        
        // 按照元素的大小从大到小排序
        // return b-a
        
        // 按照元素个位数的大小从小到大排序
        // return a%10 - b%10
        
        
        // 按照元素各位数的和的大小从大到小排序
        sumA = 0
        for(let x in String(a)){
            sumA += parseInt(String(a)[x])
        }
        
        sumB = 0
        for(let x in String(b)){
            sumB += parseInt(String(b)[x])
        }
        
        return sumB - sumA
        
    })
    console.log(scores)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值