Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to reverse a string in javascript without for loop and while loop?

I want to revers String in javascript.

The conditions is
we cant use for Loop and While loop
Posted

XML
<script>
  function funct(){
  var reversed , name = document.getElementById("t1").value;

reversed = name.split("").reverse().join("");
alert(reversed);
}
</script>

<input type="text" name="t1" id= "t1" value="yourName">
&nbsp;&nbsp;
<input type= "button" id="a" onclick="funct()" value= "button click">


It works without using JQuery as well.
Only JavaScript and pure HTML is used.
 
Share this answer
 
v2
Well, as this looks like a homework question, I'd expect that they are looking for the naive way of reversing strings, but this only properly works on none unicode strings. If you are looking for a more comprehensive solution, and it wasn't for homework, I'd use this[^] library.

So, what is the naive version? It's quite simple really:
JavaScript
var text = 'Text to reverse';
var reversed = text.split('').reverse().join('');
If you want to be really cunning, you could add this to the String prototype like this:
JavaScript
String.prototype.reverse = function(){ return this.split('').reverse().join('');}
What this functionality does is split the string into individual characters, then it reverses the order of them and finally it joins them back together again with no space in between.
 
Share this answer
 
Its will reverse your desire string.
JavaScript
var mystr     = 'sajidur';
var revstr = mystr.split('').reverse().join('');
 
Share this answer
 
v2
Did you search Google for this? :(
Ten ways to reverse a string in JavaScript[^]
 
Share this answer
 
JavaScript
var name= $('#inputfield').val();

var Reverse= name.split("").reverse().join("");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900