- Using reverse Method: we can reverse the string using the reverse method. The syntax is as follows:
var newString = "this is a test".reverse();
by using reverse methadone string “this is a test” will be reversed to xit at esat
The following code shows an example of how to use this function:
var text = "Hello World" document.write(text) document.write(reverseString(text)
// outputs: olleh dlrow
- Using Join method :
For example :
var ReverseString = str => [...str].reverse().join(''); document.write(ReverseString("hello world"))
All methods have a different mechanism for reversing the string.
If you have any doubts or queries, feel free to comment in the comment section below and I will get back to you soon.
What is your favorite method? Let me know! Mine is :
In the first line, we are using .reverse() on an array that will take all items in the list and change their order so they appear from last to first instead of starting from one end and moving towards another (which would give us “rever”).