Reverse a String in JavaScript
September 18, 2020

These solutions are from JavaScript Notes for Professionals.

Solution 1

function reverseString(str) {
 return str.split('').reverse().join('');

// "olleh"

Note: The above solution only works if does not contain surrogate pairs.

Solution 2: Using Spread operator

function reverseString(str) {
 return [...String(str)].reverse().join('');

console.log(reverseString('hello')); // "olleh"

Solution 3: Make a function using a For loop

function reverse(string) {
 var strRev = "";
 for (var i = string.length - 1; i >= 0; i--) {
 strRev += string[i];
 return strRev;
reverse("hello"); // "olleh"

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript

You might also like these

Pre-defined DatePipe format options in Angular 9AngularNATURAL JOIN in PostgresPostgresHow to host Google fonts on your server and add them using CSS?CSSSQL GROUP BY StatementSQL/MySQLNumber Properties in JavaScriptJavaScriptHow to Remove PHP File Extensions From Your Website URLs?PHPHow to Draw a Text Image using JavaScript?JavaScriptHow to vertically align a html radio button to it’s label?CSSOrder By and Group By in PostgresPostgresWhat are Web services?MiscWordPress 5.7 is migrating to latest jQuery version and people are freaking outjQueryHow does AdSense calculate page loading time?JavaScript