How to returns a passed string with letters in alphabetical order in JavaScript ? Last Updated : 09 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Let's say we need to convert the string into alphabetical order. For example: geeksforgeeks -> eeeefggkkorss Approach: The task is to create a function that takes a string and returns the alphabetical order of that string. Hence to achieve this we will go under the split, sort, and join method in javascript. Step 1: Split the given string into characters with the help of the split() method in javascript and store that in an arrayStep 2: Sort the array of characters in alphabetical order with the help of sort() functionStep 3: Join the characters into one string with the help of join() method Example: JavaScript <script> function alpha(str) { var arr = str.split(""); // splits the string res = arr.sort().join(""); // sort the array and joins to form a string return res; // returns the result } console.log("taking geeksforgeeks as a string"); console.log(alpha("geeksforgeeks")); </script> Output: taking geeksforgeeks as a string eeeefggkkorss Exceptional case: If we try to put whitespace in between our passing string it will result in putting that white space in starting of the resultant string. Like in the above example string has 1 white space and in the resultant string it is useless, so to get rid of that we can use white space regular expression i.e. \s+ for selecting white spaces and replacing it with an empty string. Example: JavaScript <script> function alpha(str) { var arr = str.split(""); res = arr.sort().join(""); rws = res.replace(/\s+/g, ""); return rws; } console.log("taking geeksforgeeks portal as a string"); console.log(alpha("geeksforgeeks portal")); </script> Output: taking geeksforgeeks portal as a string aeeeefggkklooprrsst Comment More infoAdvertise with us Next Article JavaScript Methods to Increment/Decrement Alphabetical Letters S shiv_ka_ansh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads JavaScript - Sort a String Alphabetically using a Function Here are the various methods to sort a string alphabetically using a function in JavaScript.1. Using split(), sort(), and join() MethodsThis is the most basic and commonly used method to sort a string alphabetically. The string is first converted into an array of characters, sorted, and then joined 2 min read How to return all matching strings against a regular expression in JavaScript ? In this article, we will learn how to identify if a string matches with a regular expression and subsequently return all the matching strings in JavaScript. We can use the JavaScript string.search() method to search for a match between a regular expression in a given string. Syntax: let index = stri 3 min read How to get nth occurrence of a string in JavaScript ? In this article, the task is to get the nth occurrence of a substring in a string with the help of JavaScript. We have many methods to do this some of which are described below:Approaches to get the nth occurrence of a string:Table of Content Using split() and join() methodsUsing indexOf() methodUsi 5 min read JavaScript Methods to Increment/Decrement Alphabetical Letters The task is to increment/decrement alphabetic letters using ASCII values with the help of JavaScript. Approach: Use charCodeAt() method to return the Unicode of the character at the specified index in a string.Increment/Decrement the ASCII value depending on the requirement.Use fromCharCode() method 2 min read Javascript Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i"Examples:Â Input: s = "geeks quiz practice code"Â Output: s = "code practice quiz geeks"Input: s = "getting good at coding needs a lot of practice"Â Output: s = "pra 4 min read JavaScript - Sort an Array in Reverse Order in JS JS array.sort() Method sorts the array in ascending order. We can make it descending or reverse order using array.reverse() method. Below are the following ways to sort an array in reverse order:1. Using array.sort() with array.reverse()First, sort the given array of strings using JS array.sort() me 2 min read Like