Yogesh Chauhan's Blog

Reverse a String in JavaScript

in JavaScript on 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"

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
What is Etrieve Flow?MiscellaneousHow to create a Progress Bar using JavaScript?JavaScriptCustom Post Type Template Files in WordPressWordPressCasting in PostgreSQLPostgresHow to import MySQL small sample database into phpMySQL?SQL/MySQLWhat is the difference between Loosely Typed Language and Strongly Typed Language?MiscellaneousHow to add Laravel to WordPress using Sage theme (and install Tailwind CSS)?PHPWhat are “holes” in a React component?ReactHow SCSS (Sass) finds a module without a file extension?SCSSControl rendering using CSS content-visibility propertyCSSHow to add new elements with swing animation using JavaScript and CSS?CSSHow to Write Complex Search Queries in SQL?SQL/MySQL