Languages
[Edit]
EN

TypeScript - reverse string

0 points
Created by:
Imaan-Morin
1009

In TypeScript it is possible to reverse string in following way.

1. Reverse string with split-reverse-join operations example

function reverseString(text: string): string {
  const characters: string[] = text.split('');
  const reversion: string[] = characters.reverse();
  return reversion.join('');
}

// Usage example:

const text: string = 'This is my text...';
const reversion: string = reverseString(text);

console.log(reversion); // ...txet ym si sihT

Output:

...txet ym si sihT

2. Reverse string with iteration example

function reverseString(text: string): string {
  let result: string = '';
  for (let i = text.length - 1; i > -1; --i) {
    result += text[i];
  }
  return result;
}

// Usage example:

const text: string = 'This is my text...';
const reversion: string = reverseString(text);

console.log(reversion); // ...txet ym si sihT

Output:

...txet ym si sihT

Alternative titles

  1. TypeScript - how to reverse string?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join