I am looking for a way to reverse a string without using built-in functions like split, reverse, and join. I came across this code snippet on Stack Overflow (), but I'm having trouble understanding what the code does on the fourth line. I need more clarification on that particular line. Below is the code along with some explanations:
function reverseString(str) {
reverseIt = [];
for (i = 0; i < str.length; i++) {
reverseIt = str[i] + reverseIt; // This is the first method that works
// reverseIt = str[i] + []; // I initially thought that variable "reverseIt" was equal to an empty array []
// reverseIt = str[i] + ''; // I then tried assuming that reverseIt is an empty string "", but it did not give the expected result
// var testing = []; // I attempted to create an empty array variable again
// reverseIt = str[i] + testing; // Added the above variable, but still didn't get the desired result
/*
My question is: why does the first method work? What exactly does the code on that line do?
*/
}
return reverseIt;
}
console.log(reverseString('Javascript'));