Join the Elements of the Array Using
Similarly, converting an array into a string can also be done easily with the help of predefined methods and by following some other ways. In this article, we will see the different ways in which we can convert the entire array data structure (i.e., all the elements present inside that array) into a single string.
toString(). This method does not just work with arrays but also with various other data types. Almost anything can be converted into a string using
You can add this at the end of the array to use this method, as shown below. It will take all the elements inside that array and concatenate them as a single string.
var arr = ['Google', 'is', 'no', '1', 'search engine'].toString(); console.log(arr);
Here, if you see the output, it is a string but comma-separated. Now, if you want to remove commas from the above string, you can use the
replace() method as follows. The
replace() method takes two parameters. The first parameter is the character itself that needs to be replaced (in this case, comma, which is represented by
/,), and the second parameter decides what to replace the character with (in this case, blank space). The
/ is the escape character.
"Google is no 1 search engine"
In this case, we want to remove all the commas from the string, represented by
Join the Elements of the Array Using
Another way of converting an array to a string is by using the
join() method. This method will take each element from the array and together to form a string. Here, if you directly use this method onto an array similar to that of
toString(), it will also generate a string separated by commas.
But here, you don’t need to use any other methods like the
replace() because you can directly pass any other separators as a parameter to separate the elements within the string.
var arr_1 = ['Google', 'is', 'no', '1', 'search engine'].join(); var arr_2 = ['Google', 'is', 'no', '1', 'search engine'].join("-"); var arr_3 = ['Google', 'is', 'no', '1', 'search engine'].join("##space##"); console.log(arr_1); console.log(arr_2); console.log(arr_3);
"Google,is,no,1,search engine" "Google-is-no-1-search engine" "Google##space##is##space##no##space##1##space##search engine"
JSON.stringify(value, replacer, space)
value parameter takes any value that needs to be converted into a string. This parameter is mandatory to pass. The
replacer is a function that you can pass to replace some of the elements inside the string. If you want to add white space to the output string for readability purposes, you can use the
space parameter. Both of these parameters,
space, are optional.
var arr = JSON.stringify(['Google', 'is', 'no', '1', 'search engine']); console.log(arr);
In the above example, we definitely want to use a replacer function because the output we got above is not readable. The
JSON.stringify() method directly takes individual elements of the array and converts them into the string, including its structure.
The implicit coercion is when you apply various operators (
/, and more) to the values of different types, and explicit coercion is when you use a function such as
Number(), etc. The example for both the type coercion is as shown below.
var str_1 = ['This', 'is', 11, 'clock'] + ''; var str_2 = String(['This', 'is', 11, 'clock']); console.log(str_1); console.log(str_2);
str_1 is an example of implicit coercion where we are just using an operator between two different types of values (one is an array, another is a string). The resulting output of this operation is a string. The
str_2 is an example of explicit coercion where we have just passed the entire array inside the
String() function to convert the array into a string.