reverse in javascript

To reverse a string in JavaScript, we can use the split(), reverse(), and join() methods as follows:

index.tsx
let str = "hello world";
let reversedStr = str.split("").reverse().join("");
console.log(reversedStr); // "dlrow olleh"
120 chars
4 lines

Here, we first split the string into an array of characters using the split("") method, which splits the string at each character.

Then we chain the reverse() method to reverse the order of the array elements. Finally, we use the join("") method to join the array elements back into a string, with no separator between the characters.

The result is a reversed string "dlrow olleh".

gistlibby LogSnag