String()函数 (String() function)
String() function is a predefined global function in JavaScript, it is used to convert an object to the string.
String()函数是JavaScript中预定义的全局函数,用于将对象转换为字符串。
Example:
例:
In this example, we are going to convert different object’s values to the string.
在此示例中,我们将把不同对象的值转换为字符串。
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var a = "10";
var b = "10 20";
var c = "1234 Hello";
var d = "Hello 1234";
var e = true;
var f = new Date();
//converting values to string using String() function
document.write("String(a) = " + String(a) + "<br>");
document.write("String(b) = " + String(b) + "<br>");
document.write("String(c) = " + String(c) + "<br>");
document.write("String(d) = " + String(d) + "<br>");
document.write("String(e) = " + String(e) + "<br>");
document.write("String(f) = " + String(f) + "<br>");
</script>
</body>
</html>
Output
输出量
String(a) = 10
String(b) = 10 20
String(c) = 1234 Hello
String(d) = Hello 1234
String(e) = true
String(f) = Sat Feb 02 2019 23:03:12 GMT+0530 (India Standard Time)
翻译自: https://www.includehelp.com/code-snippets/String-function-with-example-in-javascript.aspx