Hot File

Reverse a String in Javascript in Three Easiest Approaches

View: 268    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   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 .

Introduction

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 268

Comment:

To comment you must be logged in members.

Files with category

  • JUnit 5 State Of The Union using java

    View: 593    Download: 0   Comment: 0   Author: none  

    JUnit 5 State Of The Union using java

    Category: Javascript
    Fields: Other

    2.25/2 review
    JUnit 5 has been under development for about 14 months now and the prototype is almost a year old. Time to summarize what happened so far, where the project stands, and where it’s going.

  • Getting Started with Dropwizard using java

    View: 683    Download: 0   Comment: 0   Author: none  

    Getting Started with Dropwizard using java

    Category: Javascript
    Fields: Other

    2.25/2 review
    Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

  • Build Query NULL Value in MySql

    View: 312    Download: 0   Comment: 0   Author: none  

    Build Query NULL Value in MySql

    Category: Javascript
    Fields: Other

    2.5/2 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • Manage Your JavaScript Application State with MobX

    View: 328    Download: 0   Comment: 0   Author: none  

    Manage Your JavaScript Application State with MobX

    Category: Javascript
    Fields: Other

    2.25/2 review
    This article was peer reviewed by Michel Weststrate and Aaron Boyer. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

  • Build Bringing Pages to Life with the Web Animations API

    View: 339    Download: 0   Comment: 0   Author: none  

    Build Bringing Pages to Life with the Web Animations API

    Category: Javascript
    Fields: Other

    4.5/2 review
    This article is by guest author Dudley Storey. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • How to Style Google Custom Search Manually

    View: 318    Download: 0   Comment: 0   Author: none  

    How to Style Google Custom Search Manually

    Category: Javascript
    Fields: Other

    0/0 review
    Website owners very often decide on using Google’s Custom Search Engine (GCSE) for searching through their content instead of using built-in and/or custom search functionality. The reason is simple – it’s much less work, and most often it does the...

  • Test React Components Using Jest

    View: 5089    Download: 0   Comment: 0   Author: none  

    Test React Components Using Jest

    Category: Javascript
    Fields: Other

    4.5/1 review
    This article is by guest author Jack Franklin. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • Programming Question Reverse String without using String function

    View: 776    Download: 0   Comment: 0   Author: none  

    Programming Question Reverse String without using String function

    Category: Javascript
    Fields: Other

    4.5/1 review
    Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd