Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-1· AnthropicHugging Face Arena Human Preference 55K

How can I reverse a string in javascript?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with vicuna-33b’s answer.
  1. User

    How can I reverse a string in javascript?

  2. Claude Assistant

    You can reverse a string in JavaScript using the .split(), .reverse() and .join() methods:

    function reverseString(str) {
      return str.split('').reverse().join('');
    }
    
    reverseString('hello'); // 'olleh'
    

    This works by:

    • Splitting the string into an array of characters using .split('')
    • Reversing that array using .reverse()
    • Joining the array back into a string using .join('')

    So this reverses the string by reversing the order of characters in the array.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.