DEV Community

Randy Rivera
Randy Rivera

Posted on • Edited on

Combining an Array into a String Using the join Method

  • The join method is used to join the elements of an array together to create a string. It takes an argument for the delimiter that is used to separate the array elements in the string.

  • Example:

let arr = ["Playstation", "Rules"];
let str = arr.join(" ");
Enter fullscreen mode Exit fullscreen mode
  • str would have a value of the string Playstation Rules.

  • Let's use the join method inside the word function to make a sentence from the words in the string str. The function should return a string.

function sentence(str) {
  // Only change code below this line


  // Only change code above this line
}
sentence("This-is-where-the-fun-begins");
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function sentence(str) {
return str.split(/\W/).join(" ")

}
console.log(sentence("This-is-where-the-fun-begins")); will return This is where the fun begins
Enter fullscreen mode Exit fullscreen mode

Top comments (0)