You are here: irt.org | FAQ | JavaScript | Text | Q1325 [ previous next ]
JavaSscript 1.1 has array.reverse() but no string reverse.
Try this:
<script language="JavaScript"><!--
function strrev(str) {
if (!str) return '';
var revstr='';
for (i = str.length-1; i>=0; i--)
revstr+=str.charAt(i)
return revstr;
}
alert(strrev("Hello World!"));
//--></script>The following was submitted by Lee Kowalkowski
You can use split("") to convert a string to an array, reverse() to reverse the array, and join("") to convert it back.
<script language="javascript1.1"><!--
function strrev(str) {
return str.split("").reverse().join("");
}
alert(strrev("Hello World!"));
//--></script>