Home > Web Front-end > JS Tutorial > How to reverse a string in javascript

How to reverse a string in javascript

青灯夜游
Release: 2021-10-09 17:34:29
Original
12407 people have browsed it

Javascript method to reverse a string: 1. Use the "str.split("") statement to split the array according to the empty string and convert the string into a character array; 2. Use reverse() to reverse Convert the array; 3. Use the "arr.join("")" statement to convert the reversed array back to a string.

How to reverse a string in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript implements reverse string

##Idea: String to array, reverse array, array to string

split(""): Split the array based on the empty string

reverse() : Reverse the element position of the array

join(""): Convert the array back to a string without delimiters

How to reverse a string in javascript

The effect is shown in the figure:

How to reverse a string in javascript

Another method: Use the for statement to traverse the string in reverse direction

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

charAt () method can return the character at the specified position.

Output result:


How to reverse a string in javascript##[Recommended learning:

javascript advanced tutorial

The above is the detailed content of How to reverse a string in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template