- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to join JavaScript array of string
We are required to write a JavaScript function that takes in an array of strings. The function should join all the strings of the array, replace all the whitespaces with dash "-", and return the string thus formed.
For example: If the array is −
const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"];
Then the output should be −
const output = "QA-testing-promotion-Twitter-Facebook-Test";
Example
Following is the code −
const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; const joinArr = arr => { const arrStr = arr.join(''); let res = ''; for(let i = 0; i < arrStr.length; i++){ if(arrStr[i] === ' '){ if(arrStr[i-1] && arrStr[i-1] !== ' '){ res += '-'; }; continue; }else{ res += arrStr[i]; }; }; return res; }; console.log(joinArr(arr));
Output
This will produce the following output on console −
QA-testing-promotion-Twitter-Facebook-Test
- Related Articles
- Join arrays to form string in JavaScript
- Array. join() method in JavaScript
- Join in nested array in JavaScript
- How to join two arrays in JavaScript?
- Join Map values into a single string with JavaScript?
- How to convert an array into JavaScript string?
- String Join() method
- How to convert string Stream to join them in Java?
- How to parse a string from a JavaScript array?
- Convert integer array to string array in JavaScript?
- Convert nested array to string - JavaScript
- Mapping unique characters of string to an array - JavaScript
- How to convert string type value to array type in JavaScript?
- Converting string to an array in JavaScript
- Summing array of string numbers using JavaScript

Advertisements