Return a string with its characters in reverse order (e.g. "hello" -> "olleh").
Strings are immutable in JavaScript, so you build a new value. The idiomatic one-liner splits into an array of characters, reverses the array, and joins back. A manual loop from the last index is also O(n).
function reverseString(s) {
return s.split('').reverse().join('');
}
// Unicode-safe version (handles emoji/surrogate pairs):
const reverseUnicode = (s) => [...s].reverse().join('');Reversing strings underpins palindrome checks and building deterministic cache keys; as an interview opener it checks whether you know JS string/array methods.
Your reverseString('👍a') returns garbled output. Why, and how do you fix it?