Skip to content
Related Articles

Related Articles

How to convert Integer array to String array using JavaScript ?
  • Last Updated : 31 Dec, 2019

The task is to convert an integer array to the string array. Here a few of the most used techniques discussed with the help of JavaScript. In the first approach we are going to use the .toString() method with the .map() method and in the seconds approach we will use .join() method with .split() method.

Approach 1: In this approach we use .toString() method on every element of the array with the help of .map() method.

  • Example: This example uses the approach discussed above.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            How to Convert Integer array to
            String array using JavaScript ?
        </title>
          
        <style>
            body {
                text-align: center;
            }
            h1 {
                color: green;
            }
            #geeks{
                font-weight: bold;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
          
        <p id="GFG"></p>
          
        <button onclick="gfg_Run()">
            Click Here
        </button>
          
        <p id="geeks"></p>
          
        <script>
            var el_up = document.getElementById("GFG");
            var el_down = document.getElementById("geeks");
            var arr = [1, 4, 56, 43, 67, 98];
              
            el_up.innerHTML = "Click on the button to "
                    + "convert the integer array" + 
                    " to string array.<br>Array = '"
                    + arr + "' typeof(Array[0]) - "
                    + typeof(arr[0]);
              
            function gfg_Run() {
                var gfg = arr.map(function(e){
                    return e.toString()
                });
                      
                el_down.innerHTML = "Array - " + gfg + 
                    " typeof(Array[0]) - " + typeof(gfg[0]); 
            
        </script
    </body>
      
    </html>
  • Output:

Approach 2: In this approach we use .join() method which joins the array and returns it as a string. Then .split() method which splits the string on “, ” returned by join() method.

  • Example: This example uses the approach discussed above.




    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            How to Convert Integer array to
            String array using JavaScript ?
        </title>
      
        <style>
            body {
                text-align: center;
            }
            h1 {
                color: green;
            }
            #geeks{
                font-weight: bold;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
          
        <p id="GFG"></p>
          
        <button onclick="gfg_Run()">
            Click Here
        </button>
          
        <p id="geeks"></p>
          
        <script>
            var el_up = document.getElementById("GFG");
            var el_down = document.getElementById("geeks");
            var arr = [1, 4, 56, 43, 67, 98];
              
            el_up.innerHTML = "Click on the button to "
                    + "convert the integer array" +
                    " to string array.<br>Array = '" 
                    + arr + "' typeof(Array[0]) - "
                    + typeof(arr[0]);
      
            function gfg_Run() {
                var gfg = arr.join().split(', ');
                  
                el_down.innerHTML = "Array - " + gfg + 
                " typeof(Array[0]) - " + typeof(gfg[0]);
            }
        </script>
    </body>
      
    </html>
  • Output:



My Personal Notes arrow_drop_up
Recommended Articles
Page :