Reverse a String in Javascript in Three Easiest Approaches

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

0 point/0 review File has been tested

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

Reverse a String in Javascript in Three Easiest Approaches Posted on 23-09-2016  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 . 5/10 349

Comment:

To comment you must be logged in members.

Files with category

  • AngularJS and REST API

    AngularJS and REST API

    View: 84    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: 100    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: 74    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: 83    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...

  • Brief Introduction of SocketPro High Performance and Scalable Persistent Message Queue

    Brief Introduction of SocketPro High Performance and Scalable Persistent Message Queue

    View: 279    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Continuous in-line request/result batching, real-time stream sending/processing, asynchronous data transferring and parallel computation for best performance and scalability

  • Iteration Over Java Collections with High Performance

    Iteration Over Java Collections with High Performance

    View: 73    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Java developers usually deal with Collections such as ArrayList, HashSet, Java 8 come with lambda and streaming API helps us to easily work with Collections. In most cases, we work with few thousands of items and performance isn't a concern. But in...

  • SR2JLIB - A Symbolic Regression Library for Java

    SR2JLIB - A Symbolic Regression Library for Java

    View: 73    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Grammar-Guided Genetic Programming library featuring: multi-threading, just-in-time compilation of individuals, dynamic class loading, and JNI interfacing with C/C++ code

  • Yet Another Fluent JDBC Wrapper in 200 Lines of Code

    Yet Another Fluent JDBC Wrapper in 200 Lines of Code

    View: 89    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Those who are not convinced to use Hibernate to manage persistence are forced to use plain old JDBC API. Though powerful, it requires lot of typing to get it right. For example, retrieving user data from database often requires such code snippet:

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