EN
JavaScript - reverse words in a given string
0
points
In this article, we would like to show you how to reverse words in a given string in JavaScript.
Practical example
In the example below, we split the string into elements, reverse their order, and then we join them all together.
// ONLINE-RUNNER:browser;
function reverseString(string) {
return string.split(' ').reverse().join(' ');
}
console.log( reverseString('Hello world')); // world Hello
console.log( reverseString('Reverse words in string')); // string in words Reverse
Output:
world Hello
string in words Reverse