|
|
String reversal using charat and length propertyWe can reverse a string by using charat and length property of string functions in JavaScript. By using length property we will find out the total length of the string and then we will use one for loop we will print one by one character from last point using charat function.
Here is the code
<script type="text/javascript">
var my_str="Welcome to www.plus2net.com"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}
</script>
Found anything wrong or wants to improve the code by adding more features? Post your short comment here or use the Forum
| |
| | | Jim | 25-04-2010 |
|---|
| document.write(my_str.split('').reverse().join('')); | | david bandel | 26-04-2010 |
|---|
| note. the reason it's best to use str.charAt(x) rather than str[x] is that internet explorer doesn't support accessing characters of a string with array notation. |
|
|
|
|
|
|