This tutorial shows how to reverse a string in JavaScript. In JavaScript, we can use the in-built functions split()
, join()
and reverse()
to reverse a given string.
Syntax
let reverseString = string.split("").reverse().join("");
Example 1: Reverse a String
//program to reverse a string
let string = "apple"
let reverseString = string.split("").reverse().join("");
console.log(reverseString)
Output
elppa
Code Breakdown
let string = "apple"
let splitString = string.split(""); //returns [ 'a', 'p', 'p', 'l', 'e' ]
let reverseSplit = splitString.reverse(); //returns [ 'e', 'l', 'p', 'p', 'a' ]
let joinReverse = reverseSplit.join(""); //returns elppa