Reverse a string manually in JavaScript:
🧾 Using a for loop
function reverseString(str) {
let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
// Example:
console.log(reverseString("Teekam"));
// Output: "makeeT"
🧾 Using recursion
function reverseStringRecursive(str) {
if (str === "") return "";
return reverseStringRecursive(str.slice(1)) + str[0];
}
console.log(reverseStringRecursive("Teekam"));
// Output: "makeeT"
✅ Explanation
- For loop method: Start from the last character and build a new string.
- Recursive method: Take the first character, reverse the rest, and append the first character at the end.