JavaScript has no built-in String.prototype.reverse(). A common approach converts the string to an array, reverses the array, and joins it again.
const text = "plus2net";
const reversed = text.split("").reverse().join("");
console.log(reversed); // ten2sulp
const text = "plus2net";
let reversed = "";
for (let i = text.length - 1; i >= 0; i -= 1) {
reversed += text.charAt(i);
}
console.log(reversed);This preserves the original page's length and charAt() approach.
const reversed = "JavaScript".split("").reverse().join("");
console.log(reversed);
const reversed = [..."A😊B"].reverse().join("");
console.log(reversed); // B😊ASpreading is safer than split("") for surrogate-pair characters such as many emoji.
Even spread syntax works with Unicode code points, not every human-perceived character. Combining marks and complex emoji sequences may need Intl.Segmenter for truly user-visible character reversal.
function reverseString(value) {
return [...value].reverse().join("");
}
console.log(reverseString("Plus2net"));
reverse() belongs to arrays, not strings. Calling text.reverse() directly causes an error. Also remember that reversal is rarely appropriate for bidirectional-language display.
Previous / String Reference Next related topic
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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. | |
| esso | 15-04-2014 |
| this code but with Array ...??? | |
| esso | 15-04-2014 |
| Write a JavaScript code that asks the user to enter a sentence in lowercase and it will display it in UPPERCASE. For example the user enters the sentence "javascript is beautiful", and the program displays "JAVASCRIPT IS BEAUTIFUL". (Use Arrays) solution !!!??.... | |
| tenzin | 17-07-2014 |
| @esso it is really easy in an array form. function arrayReverse(){ var str = ["esso","peso"]; var arraylength = str.length;//u can use to string also. for(var i = arraylength-1; i>=0; i--){ document.getElementById("here").innerHTML += str[i]+","; } } don't forget to define <body onload="arrayReverse();"><p id="here"></p> on your HTML page. uppercase and lowercase are as simple as those above. try to do research and it will also a best way to learn more n perfect.. hope my comments are helpful to you! | |
| jacob jackson mutoya | 10-07-2015 |
| how to code html code on revering a string in javascript | |