Home > Article > Web Front-end > How to convert value to string type in javascript
Methods to convert values into strings in javascript: 1. Use the toString() method, the syntax "variable.toString()"; 2. Use the String() function, which can convert the value of the object into String, syntax "string('value')"; 3. Use " " to splice a string.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.
Convert to string type, there are three ways: Variable.toString();
, String('value');
, use '
'Splice a string;
Each method has a scope of use. We can draw the following conclusions through the following code:
In the basic data type:
( 1) Only number and boolean types call toString() for type conversion.
There is no toString() method in undefined and null.
toString() can only be used on variables, not constants.
(2) undefined and null can be converted into strings through String().
Constants can be converted to strings using String().
(3) All types can be converted into strings by concatenating strings.
In an array, you can call the toString() method and concatenate string conversion, but you cannot use String().
<script> // 转换成string类型,三种方式:变量.toString(); String('值'); 使用'+'拼接一个字符串; // number类型转string // .toString() var num =123; num = num.toString(); console.log(typeof num);//返回的是string; // String() var num2 =50; var str1 = String(num2); console.log(typeof str1);//返回string // 拼接字符串 var num3 =60; var str1s = num3 +''; console.log(str1s); console.log(typeof str1s);//返回string // 常量: // .toString() // 123.toString(); //编辑器报错,说明没有这种写法 // String() var str1 = String(123); console.log(typeof str1);//返回string // boolean类型转string // .toString() var boo =false; boo = boo.toString(); console.log(typeof boo);//返回的是string // String() var boo2 =true; var str3 = String(boo2); console.log(typeof str3);//返回string // 拼接字符串 var boo3 =false; var str3s = boo3 +''; console.log(str3s); console.log(typeof str3s);//返回string // undefined转换成string // .toString() var und; und = und.toString(); console.log(und); console.log(typeof und); //报异常: Cannot read property 'toString' of undefined // String() var und2; var str4 = String(und2); console.log(typeof str4);//返回string // 拼接字符串 var nud3; var str4s = nud3 +''; console.log(str4s); console.log(typeof str4s);//返回string // null转换成string // .toString() var nul =null; nul = nul.toString(); console.log(typeof nul); //报异常:Cannot read property 'toString' of null // String() var nul2 =null; var str5 = String(nul2); console.log(typeof str5);//返回string // 拼接字符串 var nul3; var str5s = nul3 +''; console.log(str5s); console.log(typeof str5s);//返回string // 复杂数据类型转字符串 // 数组调用toString(); var arr = [ 'hello', 'world' ]; arr = arr.toString(); console.log(arr);//输出结果为“hello,world”; console.log(typeof arr);//返回的是string // 数组调用String(); var ars = [ 'jack', 'rose' ]; ars = toString(ars); console.log(ars);//输出结果是[object Undefined] console.log(typeof ars);//返回的是string // 拼接字符串 var arr = [ 'hello', 'world' ]; arr = arr +''; console.log(arr);//输出结果为“hello,world”; console.log(typeof arr);//返回的是string </script>
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of How to convert value to string type in javascript. For more information, please follow other related articles on the PHP Chinese website!