Sorting and Reversing Characters in javascript Array

Explore our JavaScript category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different JavaScript functions that can be used in various projects according to their specific requirements.

In this JavaScript tutorial, we’ll explore how to manipulate arrays containing characters by sorting and reversing their elements.

<script>

// Define an array called charc with elements "A", "C", "B", and "D".
var charc = ["A", "C", "B", "D"];

// Print the original array to the console.
console.log("Original Array:", charc);

// Sort the array elements in lexicographical (alphabetical) order.
charc.sort();

// Print the sorted array to the console.
console.log("Sorted Array:", charc);

// Reverse the order of the array elements. charc.reverse(); // Print the reversed array to the console.
console.log("Reversed Array:", charc);

</script>

Output:

<script>
Original Array: ["A", "C", "B", "D"]
Sorted Array: ["A", "B", "C", "D"]
Reversed Array: ["D", "C", "B", "A"]
</script>