
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Join every element of an array with a specific character using for loop in JavaScript
Here we are supposed to write a function that takes in two arguments, first an array of String or Number literals, second a String and we have to return a string that contains all the elements of the array prepended and appended by the string.
For example −
applyText([1,2,3,4], ‘a’);
should return ‘a1a2a3a4a’
For these requirements, the array map() method is a better option than the for loop and the code for doing so will be −
Example
const numbers = [1, 2, 3, 4]; const word = 'a'; const applyText = (arr, text) => { const appliedString = arr.map(element => { return `${text}${element}`; }).join(""); return appliedString + text; }; console.log(applyText(numbers, word));
Output
The console output for this code will be −
a1a2a3a4a
- Related Articles
- Reduce an array to the sum of every nth element - JavaScript
- How to check an element with specific id exist using JavaScript?
- Building an array of specific size with consecutive element sum being perfect square in JavaScript
- Maximum possible XOR of every element in an array with another array in C++
- C++ Program to fill an array with a specific element
- Swift Program to fill an array with a specific element
- Golang program to fill an array with a specific element
- How to remove every Nth element from an array JavaScript?
- Loop through an index of an array to search for a certain letter in JavaScript
- Adding an element in an array using Javascript
- Finding sum of every nth element of array in JavaScript
- Why is using “for…in” loop in JavaScript array iteration a bad idea?
- Golang Program To Convert An Array Into A String And Join Elements With A Specified Character
- Swift Program to convert an array into a string and join elements with a specified character
- Program to loop on every character in string in C++

Advertisements