JavaScript – Convert Array of Characters to a String
To convert given array of characters to a string in JavaScript, use Array.join()
method.
join()
method takes a delimiter string, and joins the elements of the array (array of characters), and returns the resulting string. Since we need no delimiter string, pass an empty string as argument to join(
) method.
The syntax of the statement to convert an array of characters charArray
to a string str
is
</>
Copy
var str = charArray.join("");
Example
In the following example, we take an array of characters in charArray
, join them to a string using Array.join()
method, and display the resulting string in pre#output.
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var charArray = ['a', 'p', 'p', 'l', 'e'];
var str = charArray.join("");
document.getElementById('output').innerHTML = str;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to convert a given array of characters to a string in JavaScript using Array.join()
method, with example program.