Sort-1

 

.sort() function will sort array of strings in JavaScript. We have an array of strings like var arr= ['ABC', 'XYX', 'BCD'] To sort this array, str.sort() will sort the array of string.

 
 
 
<!DOCTYPE html> <html> <head> <title>Welcome to LearnKode - A code learning platform</title> </head> <body> <button onclick="sortArray()">Sort the array</button> <p id="sample"></p> <script> var arr= ['ABC', 'XYX', 'BCD']; document.getElementById("sample").innerHTML = arr; function sortArray() { fruits.sort(); document.getElementById("sample").innerHTML = arr; } </script> </body> </html>
Output