Loading...

Reverse a String in Javascript in Three Easiest Approaches

View: 393    Dowload: 0   Comment: 0   Post by: hanhga   Category: Javascript   Fields: Other

How to reverse a string in Javascript. This question is asked by Shashank on my WebRewrite Facebook page.  Let’s check the solution of this problem .

Reverse a String in Javascript – Method 1

Javascript does not provide direct method for string reversal. Using little trick we can reverse a string in javascript by using in-built functions.

Here i am using split, reverse and join method.

var str= "webrewrite.com";
 
var result = str.split("").reverse().join("");
 
console.log(result);
 
/* Output */
 
moc.etirwerbew

Above snippet split the string into individual character array, then it reverses the character array and finally it joins them back together with no space in between.

Let’s check step by step.

/* First string is converted into an array using split method. */
 
console.log(str.split(""));
 
["w", "e", "b", "r", "e", "w", "r", "i", "t", "e", ".", "c", "o", "m"]
 
/* Then array reverse method reversed the array. */
 
console.log(str.split("").reverse());
 
["m", "o", "c", ".", "e", "t", "i", "r", "w", "e", "r", "b", "e", "w"]
 
/* After the array is reversed join them using join method. */
 
console.log(str.split("").reverse().join(""));
 
/* Final output reverse string. */
 
moc.etirwerbew

Create function.

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

Add Reverse Method to String Prototype

String.prototype.reverse = function() {
  return this.split('').reverse().join('');
}
 
var str = "webrewrite.com";
 
/* Call Reverse Method. */
 
console.log(str.reverse());
 
/* Output */
 
moc.etirwerbew

Reverse String in Javascript – Custom Method

i) Using the swapping of index value.

function reverse(str) {
 
  /* Convert the string into array. */
  str = str.split('');
  
  /* Take the length. */
  var len = str.length,      
      //mid index
      midValue = Math.floor(len / 2) - 1,
      tmp;
 
  /* Swap the position. */
 
  for (var i = 0; i <=midValue ; i++) {
    tmp = str[len - i - 1];
    str[len - i - 1] = str[i];
    str[i] = tmp;
  }
 
  /* Convert the array into string. */
 
  return str.join('');
}
 
/* Calling reverse method. */
 
var revStr = reverse("webrewrite");
console.log(revStr);

Reverse a String in Javascript in Three Easiest Approaches

How to reverse a string in Javascript. This question is asked by Shashank on my WebRewrite Facebook page. Let’s check the solution of this problem .

Posted on 23-09-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Mini Youtube Using ReactJS

    Mini Youtube Using ReactJS

    View: 25    Download: 2   Comment: 0

    Category: Javascript     Fields: none

    This is one the best starter for ReactJS. MiniYoutube as the name suggests is a youtube like website developed using reactJS and youtube API. This project actually let's you search , play and list youtube videos. Do check it out and start learning...

  • Angular 6 Starter with Laravel 5.6 API Service

    Angular 6 Starter with Laravel 5.6 API Service

    View: 62    Download: 0   Comment: 0

    Category: Javascript     Fields: none

    Angular 6 and Laravel 5.6 This project is a starter for creating interface with Angular using bootstrap && css && sass and using Laravel 5.6 for api requests. Demo Installation This project is divided in two parts (projects) and before use them you...

  • Simple Richtext Editor Based on pellJS

    Simple Richtext Editor Based on pellJS

    View: 26    Download: 0   Comment: 0

    Category: Javascript     Fields: none

    A simple visual editor for websites using the pell javascipt. It also has the option to switch between visual editor mode and source code mode. I will upload an update for new functionality soon. Source Code Editor Visual Editor

  • Data Visualization for BI: How to Design Layouts for .NET Financial Reports

    Data Visualization for BI: How to Design Layouts for .NET Financial Reports

    View: 37    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    With the Active Reports Server, you can have a multi-tenant environment where users from various departments, companies, or other specifications can log in, view their reports (and only their reports), export the data, or set up a distribution...

  • AngularJS and REST API

    AngularJS and REST API

    View: 204    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    This is a tutorial for those interested in a quick introduction to AngularJS and REST API. We will build the familiar Periodic Table of the Elements found in every chemistry textbook, and allow the user to select a Chemical Element by clicking on...

  • Collective Intelligence, Recommending Items Based on Similar Users' Taste

    Collective Intelligence, Recommending Items Based on Similar Users' Taste

    View: 168    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Using Collaborative Filtering to find people who share tastes, and for making automatic recommendations based on things that other people like.

  • Think Like a Bird for Better Parallel Programming

    Think Like a Bird for Better Parallel Programming

    View: 157    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Coding an application to run in parallel is hard, right? I mean, it must be hard or we’d see parallel programs everywhere. All we'd see are slick parallel apps that use every available core effortlessly. Instead multi-threaded apps are the exception...

  • Getting Started with the Bing Search APIs

    Getting Started with the Bing Search APIs

    View: 169    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Bing Search API is a set of REST interfaces that find web pages, news, images, videos, entities, related searches, spelling corrections, and more in response to queries from any programming language that can generate a web request. Applications that...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd