In JavaScript, use the “join()” method along with the “filter()” method to join the non-empty strings with a separator. The filter() method is used to add the non-empty strings to an array before using the join() method to join them with a separator because the join() method only joins strings, including empty strings.
This article will describe the procedure to combine non-empty JavaScript strings with a separator.
How to Join/Combine non-Empty JavaScript Strings With a Separator?
The “join()” method is used to join strings in JavaScript, but it does not eliminate the empty strings; it only combines them. So, to eliminate empty strings and combine only non-empty strings with a separator, first, add the strings to an array and call the “filter()” method with the join() method. The filter() method filters only true values (non-empty strings) from an array.
Syntax
For joining non-empty strings with a separator, use the given syntax:
In the above syntax:
-
- First, add the strings to an array using the Array.filter() method.
- The filter method takes a function and calls it for each element in the array. It will return an array of no empty strings.
- The filter method uses a Boolean object as a function, which converts all false values (empty strings) to false and all true values (non-empty strings) to true.
- At the last, use the Array.join() method to join the array elements.
- The array elements in an array are joined into a string by the join method, which accepts a separator as an argument.
Example
In the given example, first, create five strings, two of which will be empty:
var string2;
var string3 = "Learn";
var string4 = "";
var string5 = "JavaScript";
Now, join all the strings with a separator, first pass all the strings in an array and then call the “filter()” method with the “join()” method and store the result in variable “nonEmptyStringArray”:
.filter(element => Boolean(element)).join(',');
Print the result on the console using the “console.log()” method:
The output indicates that using the “filter()” method with the “join()” method, non-empty strings are successfully joined with a separator:
Now, let’s see what will happen when we use only the “join()” method for joining the non-empty strings.
Join all the strings by passing them in an array and call the “join()” method with a separator (“,”):
Print the result on the console:
The output indicates that the “join()” method joins all the strings including empty strings. That’s why for joining non-empty strings, we use the “filter()” method with the “join() “method:
We have provided the essential instructions related to joining the non-empty strings with a separator.
Conclusion
To join the non-empty strings with a separator, use the “filter()” method with the “join()” method. The join() method only joins the strings, including empty strings, so to eliminate empty strings, use the filter() method that will add the non-empty strings to an array and join them with a separator using the join() method. This article describes the procedure to combine non-empty JavaScript strings with a separator.