JavaScript - reverse words in a given string

0 points
Created by:
Dirask Admin - Marwin

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.

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

JavaScript - String (popular problems)

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.

❤️💻 🙂