Strings are immutable in JavaScript. Hence, even if we try to overwrite it but original value will never be lost. In order to reverse using this method, we need to convert string into an array first.
function reverse(str) {
let i;
let temp;
const string = str.split("");
const limit = parseInt(string.length / 2, 10);
for (i = 0; i < limit; i++) {
temp = string[i];
string[i] = string[string.length - i - 1];
string[string.length - i - 1] = temp;
}
return string;
}
const reversed = reverse("hello");
console.log(reversed);
P.S. This is not the only way to reverse a string. There are multiple ways to do it.
For useful and amazing frontend and programming tutorials: https://bit.ly/devtools-yt