Warning: session_start(): open(/tmp/sess_3595912c78a761962d469f3374659494, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to convert array to string with spaces in JavaScript - LearnShareIT

How to convert array to string with spaces in JavaScript

Convert Array to String with Spaces in JavaScript

You will know how to convert array to string with spaces in JavaScript using a couple of methods: join() and _.join(). These skills must be taken into consideration if you are working with strings in Javascript.

Convert array to string with spaces in JavaScript

Using join()

The join() method will convert an array of substrings into one string with a default separator as a comma.

Syntax:

join(separator)

Parameter:

  • separator: A string used to separate between elements.

Assume you already have an array of strings. All you have to do now is call this method on that array with a pass-by-value parameter as a space character:

let arr = ['Learn', 'Share', 'IT'];

// Convert array to string separated by 1 space
let res = arr.join(' ');
console.log(res);

// Convert array to string separated by 3 spaces
res = arr.join('   ');
console.log(res);

Output: 

Learn Share IT
Learn   Share   IT

The above example shows that we have an array containing 3 words. We then apply the join() method and then get the result as a concatenated string

Using _.join() 

This method comes from a library named lodash. You have to import it before use; just add this <script> tag at the end of your body tag.

The syntax of this method has a small difference from the join() method:

Syntax:

_.join(array, separator)

  • array: the array which you want to convert to a string
  • separator: a string used to distinguish between substrings, will be a comma (,) if not given.
<html>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
    <script type="text/javascript">
      let arr = ['Learn', 'Share', 'IT'];

      // Convert array to string separated by a space
      let res = _.join(arr,' ');
      alert(res);
    </script>
  </body>
</html>

Output: 

Learn Share IT

Have you recognized the differences between these two methods? In the first method, we only need 1 argument, which is a separator. In the second one, we need to pass an array and a separator in order to work. Although this solution may need to import an external library, it also produces the same results as the first one.

Summary

We have discussed many ways to convert array to string with spaces in JavaScript using a couple of different methods. We suggest you use the first solution of using the join() method. With the use of our guide in this tutorial, you can be successful in achieving the task.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *