Search This Blog

2026/10/15

Javascript: Array Sorting Guess output of the Code




Guess output of code.


Code

let arr =[1,21,30,4]
arr.sort((a,b)=>{
 return b-a
})
console.log(arr)




Here sorting will be done element are treated as number & sorting is descending.
output will be [ 30, 21, 4, 1 ]


let arr =[1,21,30,4]
arr.sort()
console.log(arr)


Here sorting will be done in lexographic order ascending where each element is trated as string

output will be [1,21,30,4]

No comments:

Post a Comment