'How to reverse a string in JavaScript using a "for...in" loop?
I have a piece of JavaScript code using a for loop to reverse a string. However, I would like to know if it is possible to use a for in loop instead and how would I go about that?
function reverse(str){
var reversedString = '';
for (var i = str.length - 1; i >= 0; i--){
reversedString = reversedString + str[i];
}
return reversedString;
}
alert(reverse('hello'));
Solution 1:[1]
From MDN Docs
for...in should not be used to iterate over an Array where the index order is important.
Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for...in will return the indexes in any particular order. The for...in loop statement will return all enumerable properties, including those with non–integer names and those that are inherited.
Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.prototype.forEach() or the for...of loop) when iterating over arrays where the order of access is important.
let iterable = '1234567';
String.prototype.hello = "hello"; // doesn't affect
let reversed = "";
for (let value of iterable) {
reversed = value + reversed;
}
console.log(reversed);
Feature Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 38 12 131 No 25 8
Break for..in (see the hello creeping in)
let iterable = '1234567';
String.prototype.hello = "hello";
let reversed = "";
for (let index in iterable) {
reversed = iterable[index] + reversed;
}
console.log(reversed);
Solution 2:[2]
Even using the arrow function you could reverse a string.
const reverseString = (str) => str.split("").reverse().join("");
console.log(reverseString("hello"))
Solution 3:[3]
You shouldn't use for...in in this case. for...in was designed for iterating through object literals, not for strings or arrays.
[...'mystring'].reverse().join('')
Array.from('mystring').reverse().join('')
I recommend you to use spread syntax or Array.from to convert strings into arrays instead of .split('') since split('') won't work in the case of multibytes unicode characters.
console.log([...'\uD83D\uDE80']);
console.log('\uD83D\uDE80'.split(''));
Solution 4:[4]
No need to use for in
'mystring'.split('').reverse().join('')
Solution 5:[5]
You can used split() & reduce() functions
let str = "hello";
return str.split('').reduce((rev,char) =>
char + rev,
'');
Solution 6:[6]
Reverse string using while loop
function reverseStr(word) {
let reversed = '';
let i = 0;
while (i < word.length) {
reversed = word[i] + reversed;
i++;
}
return reversed;
}
Solution 7:[7]
function reverse(str) {
let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
console.log("stefana")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Anand Wadsinge |
| Solution 3 | |
| Solution 4 | Fan Cheung |
| Solution 5 | |
| Solution 6 | Anastasia Lukavsky |
| Solution 7 | Kai - Kazuya Ito |
