<!DOCTYPE html> <html> <body> <h1>JavaScript Arrays</h1> <h2>The sort() Method</h2> <p>The sort() method sorts an array alphabetically:</p> <p id="workingcode1"></p> <p id="workingcode2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("workingcode1").innerHTML = fruits; fruits.sort(); document.getElementById("workingcode2").innerHTML = fruits; </script> </body> </html>
Output:
JavaScript Arrays
The sort() Method
The sort() method sorts an array alphabetically:
Banana,Orange,Apple,Mango
Apple,Banana,Mango,Orange
Sort() function in JavaScript
Expert Asked question July 17, 2023