CodeNewbie Community

Cover image for Examples for reversing a string in JavaScript, Python and Dart
Andrew Baisden
Andrew Baisden

Posted on

Examples for reversing a string in JavaScript, Python and Dart

One of the most common interview questions is how do you reverse a string in the programming language that you know. Here are a few examples of how easily it can be done. As these programming languages are C based the syntax is quite similar so it is easy to translate across languages.

Example 1

JavaScript

function reverse(str) {
        console.log(str.split('').reverse().join(''));
}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
  print(''.join(reversed(str)));


reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    print(str.split('').reversed.join(''));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Example 2

JavaScript

function reverse(str) {
    reversed = '';

    for (let char of str) {
        reversed = char + reversed;
    }
     console.log(reversed);

}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
  reversed = '';
  for char in str:
    reversed = char + reversed;

  print(reversed);


reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    List newStrings = [];
    dynamic index = (str.length);

    while (index > 0) {
      index -= 1;
      newStrings.add(str[index]);
    }
       print(newStrings.join(""));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Example 3

JavaScript

function reverse(str) {

    const arr = [];
    for (let i = str.length - 1; i > -1; i--) {
        arr.push(str[i]);
    }
    console.log(arr.join(''));
}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
    new_strings = []
    index = len(str)
    while index:
        index -= 1                       
        new_strings.append(str[index])
    print(''.join(new_strings))

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    List arr = [];
    for (var i = str.length - 1; i > -1; i--) {
      arr.add((str[i]));
    }
    print(arr.join(''));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Example 4

JavaScript

function reverse(str) {

    const arr = [];
    for (let i = str.length - 1; i > -1; i--) {
        arr.push(str[i]);
    }

  // Using regex
    console.log(arr.toString().replace(/,/g, ''));

  // Using reduce
    console.log(arr.reduce((acc, cur) => acc + cur));

}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
  print(str[::-1]);


reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    List arr = [];
    for (var i = str.length - 1; i > -1; i--) {
      arr.add((str[i]));
    }

    // Using Reduce
    print(arr.reduce((acc, curr) => acc + curr));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

Collapse
jordanaf808 profile image
jordanaf808

are there benefits to using one of these methods over the other?

Collapse
andrewbaisden profile image
Andrew Baisden Author

Some of them have less lines of code which could lead to a slightly smaller file size and performance boost. The biggest takeaway here would be showing that you understand the programming language well as you can perform the same function using different methods.