Write a JavaScript program to reverse a string

Description: Reverse the string “JavaScript”.

Hint:

  • Split the string into an array of characters, reverse it and join it back.

JavaScript program to reverse a string

const text = "JavaScript";

// 1. split, reverse, join
console.log(text.split("").reverse().join(""));

// 2. loop
let result = "";
for (const ch of text) {
  result = ch + result;
}
console.log(result);

Output:

Tutorials Class - Output Window

tpircSavaJ
tpircSavaJ

How it works

  • Strings cannot be reversed directly, because they have no reverse() method, but arrays do.
  • In the loop each new character is added to the front of the result.

Read JavaScript Strings.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.