如何在JavaScript中将值转换为字符串?
有 5种将值 转换为字符串的方法。他们是
连接空字符串
模板字符串
JSON。串化
toString()
串()
示例
在以下示例中,所有上述方法都用于将值转换为字符串,并显示最终结果,如输出所示。
<html>
<body>
<script>
const value = 123;
document.write((value + '') +" "+typeof (value + ''));
document.write("</br>");
document.write((`${value}`) +" "+typeof (`${value}`));
document.write("</br>");
document.write((JSON.stringify(value)) +" "+typeof (JSON.stringify(value)));
document.write("</br>");
document.write((value.toString()) +" "+typeof (value.toString()));
document.write("</br>");
document.write((String(value)) +" "+typeof(String(value)));
</script>
</body>
</html>输出结果
123 string 123 string 123 string 123 string 123 string
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短