Home  >  Article  >  Web Front-end  >  Three methods to achieve string reversal in javascript_javascript skills

Three methods to achieve string reversal in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:12:411788browse

The first method

Copy code The code is as follows:

var str = "abcdef";
console.log( str.split("").reverse().join("") );

Second method:
Copy code The code is as follows:

var str="abcdef"
var i=str.length;
i=i- 1;
for (var x = i; x >=0; x--)
{
document.write(str.charAt(x));
}

Third method:
Copy code The code is as follows:


<script> <br>function reverse(str) <br>{ <br> if(str.length == 0)return null; <br> var i = str. length; <br> var dstr = ""; <br> while(--i >= 0) <br> { <br> dstr = str.charAt(i); <br> } <br> return dstr; <br>} <br>var str = "abcdef"; <br>str = reverse(str); <br>document.write(str); <br></script>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn