Sort JavaScript Array
Basic of the code:
const: const is the built-in keyword of javascript language. The const keyword is used to constant a variable or variables or a function, which will not be changed during the code running process.
sort(): This is a built-in function of the variable class of javascript language. The sort() function is used to sort an array or string-based array in ascending order.
reverse(): This is also a built-in function just like the sort() function but works against the sort() function. It sorts an array in descending order.
console.log: A print function used to give the output in the console of a browser.
//Body of the code
const language = ["Python", "C", "C++", "JavaScript"];
//Ascending order
language.sort();
console.log(language)
//descending order
language.reverse();
console.log(language)
//End of the code
Sample Input:
"Python", "C", "C++", "JavaScript"
Sample Output:
"C", "C++", "JavaScript", "Python"
Read Similar Post: JavaScript Sort Objects by Property