In this tutorial I will talk about how you can reverse a string in JavaScript language.
This is a very popular question i.e. asked in interviews. There are many ways in which we can reverse a string. Here I will discuss different ways in which we can achieve this.
JavaScript Reverse String
Using Buitin Function
To reverse a string using built-in methods we have to chain some methods like join, split and reverse.
For this first, we need to convert the string into an array by using split method then after this, we have to reverse that array then join the array using join method. You can check the below example.
let Str="Hello"; let spltStr=Str.split(""); let reverseStr=spltStr.reverse(); let joinStr=reverseStr.join(""); console.log(joinStr);
You can do this in one line as given below.
function reverseString(str) { return str.split("").reverse().join(""); } reverseString("hello");
You can also create array instance by using Array.from inbuilt function.
First, use Array.from() to turn a string into an array, then Array.prototype.reverse() to reverse the array, and then Array.prototype.join() to make it back a string.
const reverseStr = str => Array.from(str).reverse().join(''); console.log(reverseStr('hello'));
Using Loops
Loop is a process of repeating a block of code until a specific condition is met. So, we can reverse a string using for or while loop.
For loop:
function reverseStr(str) { var newString = ""; for (var i = str.length - 1; i >= 0; i--) { newString += str[i]; } return newString; } console.log(reverseStr('helloWorld'))
While loop:
var name = "Hello"; function reverseString(sampleStr) { if(!sampleStr.trim() || 'string' !== typeof sampleStr) { return; } let strLength=sampleStr.length, s=''; while(strLength > 0) { strLength--; s+= sampleStr[strLength]; } return s; } console.log(reverseString(name))
Using Recursion
Recursion is a process in which function called itself either directly or indirectly.
function reverseString(str) { if (str === "") return ""; else return reverseString(str.substr(1)) + str.charAt(0); } console.log(reverseString('hello World'))
Using Ternary Operator
It is just a shorthand version of if conditional statement. In this operator, it requires at least 3 operands. You can increase the operators according to condition. Here I have used the substr() method and charAt method. They are inbuilt methods in javascript.
Substr method returns the characters in a string beginning at the specified location through the specified number of characters.
“hello”.substr(1); // “ello”
The charAt() method returns the specified character from a string.
“hello”.charAt(0); // “h”
function reverseStr(str) { return (str === '') ? '' : reverseStr(str.substr(1)) + str.charAt(0); } console.log(reverseStr("hello"))
I hope you got the ideas of how to reverse a string in JavaScript. Comment down below if you have queries or know any other way to do this.
The post JavaScript Reverse String – 4 Ways appeared first on The Crazy Programmer.
No comments:
Post a Comment