如何在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 入秋卖茶叶祝福语简短