To reverse a string in JavaScript, you can use the built-in split()
, reverse()
, and join()
methods.
In this example,
str1
with a value of "Hello World"
.str1
into an array of characters using split('')
.reverse()
.join('')
and print the result.let str1 = 'Hello World';
let reversedStr1 = str1.split('').reverse().join('');
console.log('Reversed string is: ' + reversedStr1);
Reversed string is: dlroW olleH
In this example,
str2
with a value of "apple"
.str2
into an array of characters using split('')
.reverse()
.join('')
and print the result.let str2 = 'apple';
let reversedStr2 = str2.split('').reverse().join('');
console.log('Reversed string is: ' + reversedStr2);
Reversed string is: elppa
In this tutorial, we learned How to Reverse a String in JavaScript language with well detailed examples.