如何在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 简短祝福语毛笔字