[Solved] Reverse the String in JavaScript [duplicate]


I call the split function passing dash which separate each part of the string

str.split("-").reverse().join("-");

Description of functions used

  1. String.prototype.split(): The split() method turns a String into an array of strings, by separating the string at each instance of a specified separator string.
const chaine = "Text";
console.log(chaine.split('')); // output ["T", "e", "x", "t"]
  1. Array.prototype.reverse(): The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
const characters = ["T", "e", "x", "t"];
console.log(characters.reverse()); // output ["t", "x", "e", "T"]
  1. Array.prototype.join(): The join() method creates and returns a new string by concatenating all of the elements in an array
const reverseCharacters = ["t", "x", "e", "T"];
console.log(reverseCharacters.join('')); // output "txeT"

solved Reverse the String in JavaScript [duplicate]