JavaScript - reverse words in a given string

0 points
Created by:

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


world Hello
string in words Reverse


  1. Array.prototype.reverse() - MDN Docs

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (popular problems)

JavaScript - reverse words in a given string
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂