js - 05 JS数据类型-字符串数据类型

字符串数据类型

所有用单引号、双引号、反引号(撇 ES6模版字符串)抱起来的都是字符串

把其他类型值转换为字符串

  • [val].toString()
  • 字符串拼接

[val].toString()
let a = 12;
console.log(a.toString())       // => '12'
console.log((NaN).toString())   // => 'NaN'
console.log(ture.toString())    // => 'true'
console.log(false.toString())   // => 'false'
console.log(null.toString())    // => Uncaught TypeError:能用,但禁止
console.log(undefined.toString())    // => Uncaught TypeError:能用,但禁止

null/undefinned .toString() 能使用,但是会报错,其转换为字符串的结果就是'null'/'undefinned'
  • 普通对象({xxx:‘xxx’}).toString() => ‘[object, Object]’
    因为:Object.prototype.toString方法不是转换为字符串,而是用来检测数据类型

字符串拼接
  • 在四则运算中,除加法之外,其余都是数学计算,只有加法可能会存在字符串拼接(一旦遇到字符串,则不是数学运算,而是字符串拼接)
console.log('10' + 10)      // => 1010
console.log('10' - 10)      // => 0
console.log('10px' - 10)    // => NaN

-- 计算console.log(a)
let a = 10 + null + true + [12] + undefined + 'xxx' + null + [] + 10 + false

解:10 + null => 10 + 0 => 10
    10 + true => 10 + 1 => 11
    11 + [] => 11 + '' => '11'
    '11' + undefined => '11undefined'
    ……
    解得: 
  console.log(a) => '1112undefinedxxxnull10false'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值