Last Updated : Jan 1, 2023
Let us see the example below for a better understanding with help of codes.
- With the help of the toString function
- With the help of the join function
Let us see the examples given below of both of them for a quick view.
By Using toString Function
<p id="result"></p> <script> const colors = ["Red", "Yellow", "Green", "Blue"]; let string_here = colors.toString(); document.getElementById("result").innerHTML = string_here; </script>
By Using Join Function
<p id="result"></p> <script> const cars = ["Mercedes", "Audi", "BMW", "Bantley"]; let resulted_string = cars.join(); document.getElementById("result").innerHTML = resulted_string; </script>
- In previous tutorials, we already many times create a full code example in which we first create an HTML form with its basic structure. Inside that structure, we use HTML, head, title, body, and script tags.
- In the first example, we see that a paragraph tag is created with an id. After that, we create our Script tag. A script tag is paired tag and is used to write scripts in between the opening and closing tag.
- Here, we create a constant in which we store our array. inside the array, we store the values of colors in our first example. In the next line, we use our toString() function on our constant in which we store our array. The result of that function is stored inside a variable. Now, our job is to display the result so that we can see what is output and this is done using the paragraph tag which we created.
- In our next example, that is done with the help of the join function. Here we have done the same process as the example of the toString function. But the difference here is that we used the join function instead of the toString function.
- One thing to note here is that we can use the join function in many ways. As the join function is used join arrays can be done with the help of connectors. Instead of Commas as connectors, we can use any symbol like *, &, $, +, etc., and any text to convert arrays to strings.