Home > Article > Web Front-end > Three ways to convert JS to String
Methods to convert JS to String: 1. Use the "toString()" method, the syntax is "array name.toString()"; 2. Use the "String()" method, the syntax is "String(array name) "; 3. Use the null character, the syntax is "array name" "operator and null character".
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.
Convert to string type, there are three ways: variable .toString()
; String('value')
; Use ' ' to splice a string;
Each method has a scope of use. We can draw the following conclusions from the following code:
Among the basic data types:
Only number, boolean type calls toString() for type conversion. There is no toString() method in undefined and null. toString() can only be used on variables, not constants.
undefiend and null can be converted into strings through String(). Constants can be converted to strings using String().
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().
1. toString()
var num = 8; var numString = num.toString(); console.log(numString); var result = true; var resultString = result.toString(); console.log(resultString); // toString 中的参数代表进制 console.log(num.toString(10))
2. String()
var num1 = 8; var num2 = String(num1); console.log(num2);
3. Splice an empty character ""
console.log(num1 + "");
[Recommended learning: javascript video tutorial]
The above is the detailed content of Three ways to convert JS to String. For more information, please follow other related articles on the PHP Chinese website!