reverse a string in javascript

To reverse a string in javascript, you can use the split(), reverse() and join() methods.

Here's the code:

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

Output:

index.tsx
dlrow olleh
12 chars
2 lines

In the above code, the split("") method is used to split the string into an array of characters, then the reverse() method is used to reverse the order of the characters in the array, and finally the join("") method is used to join the characters back together into a string.

related categories

gistlibby LogSnag