Convert Array To A String With Spaces In JavaScript

To convert array to a string with spaces in JavaScript, use the ‘join()’ method. The ‘join()’ method is used to join the elements of an array into a string by taking a ” ” as a parameter.

convert array to a string with spaces in javascript

Convert Array To A String With Spaces In JavaScript

To convert array to a string with spaces in JavaScript, you can use the ‘join()‘ method. Pass a ” ” as a parameter to the ‘join()’ method and it will insert spaces between each array element when converting it to a string.

Here is an example –

var stringArray = ["JavaScript", "Convert", "Array", "to", "String"];

// Use the join() method to convert the array to a string with spaces.

var result = stringArray.join(" ");

console.log(result);

// Output: "JavaScript Convert Array to String"

As you can see, the ‘join()’ method takes a ” ” as a parameter and returns a string with spaces between each array element.

Note: The ‘join()’ method is not only used for converting arrays to strings with spaces. You can also use it to convert arrays to strings without spaces, or any other character for that matter. Simply pass the character you want to use as a parameter to the ‘join()’ method and it will use it as a separator when converting the array to a string.

Here is an example –

var stringArray = ["JavaScript", "Convert", "Array", "to", "String"];

// Use the join() method to convert the array to a string with no spaces.

var result = stringArray.join("");

console.log(result);

// Output: JavaScriptConvertArraytoString

As you can see, the ‘join()’ method takes an empty string as a parameter and returns a string with no spaces between each array element.

Here is an example of using the join() method to convert an array to a string with – as a separator –

var stringArray = ["JavaScript", "Convert", "Array", "to", "String"];

// Use the join() method to convert the array to a string with – as a separator.

var result = stringArray.join("-");

console.log(result);

// Output: JavaScript-Convert-Array-to-String

As you can see, the ‘join()’ method takes a – as a parameter and returns a string with – as a separator between each array element.

If you use the method on an empty array, it will return an empty string.

Here is an example of using the ‘join()’ method on an empty array –

var emptyArray = [];

// Use the join() method to convert the empty array to a string.

var result = emptyArray.join(" ");

console.log(result);

// Output: "";

As you can see, the ‘join()’ method returns an empty string when used on an empty array.

If one of the elements of the array is null or undefined, it will be converted to an empty string when using the ‘join()’ method.

Here is an example of using the ‘join()’ method on an array with a null element –

var nullArray = ["JavaScript", "Convert", "Array", "to", "String", null];

// Use the join() method to convert the null array to a string.

var result = nullArray.join(" ");

console.log(result);

// Output: JavaScript Convert Array to String

As you can see, the ‘join()’ method returns a string with an empty space in place of the null element.

If you use the ‘join()’ method on an array with a non-string element, it will be converted to a string before being joined.

Here is an example of using the ‘join()’ method on an array with a number element –

var numberArray = ["JavaScript", "Convert", "Array", "to", "String", 10];

// Use the join() method to convert the number array to a string.

var result = numberArray.join(" ");

console.log(result);

// Output: JavaScript Convert Array to String 10

As you can see, the ‘join()’ method returns a string with the number element converted to a string before being joined.

Conclusion

The ‘join()’ method is a convenient way to convert array to a string with spaces in JavaScript, or any other character for that matter. Simply pass the character you want to use as a parameter to the ‘join()’ method and it will use it as a separator when converting the array to a string.

Leave a Reply