把值转换成字符串的四种方法

Posted on 2020-09-28 00:59  猫头唔食鱼  阅读(686)  评论(0编辑  收藏  举报
 1.toString  不适用于undefined 和 Null
  2. value + '' 
  3. String(value)
4.JSON.stringify()
 
  console.log(JSON.stringify(5)); // '5'
    console.log(JSON.stringify(NaN)); // 'Null'
    console.log(JSON.stringify(null)); // 'Null'
    console.log(JSON.stringify(undefined)); // undefined 不是字符串undefined
    console.log(JSON.stringify(false)); // 'false'