- Convert the string to an array of characters using the “split()” method.
- Reverse the array using the “reverse()” method.
- Join the reversed array back into a string using the “join()” method.
const inputString = 'redrum'; const reversedString = inputString.split('').reverse().join(''); console.log(reversedString);
In this example, we first used the split(”) method to convert the inputString variable (‘redrum’) into an array of characters.
In the next step, we used the reverse() method to reverse the order of the array elements.
Finally, we used the join(”) to concatenate the reversed array elements into a string.
The resulting reversed string is ‘murder’.
Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.