Help needed - "nums.sort is not a function" error

I was trying to sort an array of nums with function call.

var removeDuplicates = function(nums) {
   return nums.sort(); 
};
let sorted = removeDuplicates(1,2,1,4);
console.log(sorted);

I am getting the error "nums.sort is not a function " why is that can someone explain?

@Chirashankar6 You passed 4 arguments to your removeDuplicates function. None of them was an array. If you would have passed an array with 4 numbers in it, it would have sorted the 4 numbers. Make sure when you are using the sort method that you use a callback function to define how you want to sort the array.

Can you please tell me how to do a callback functionality?
I understood the first part where you told we need to pass the parameter as array and then the arguments would work.

Hello Chriashankar6,

To understand how sort works, I would recommend you to read this page.

In particular this example:

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

When you use the sort method, you can pass it a function with two parameters. It allows telling the sort method how you want it to sort things (ascending order, descending order, etc…)

Hi, regarding the callback function can pass whatever function that will hold the sorting logic into the sort function. You can even do.sort() without any function - just read it up on mdn.
Example, i could randomise with Math.random

[1, 2, 3, 4,5, 6].sort(() => Math.random - 0.5)

[2, A, a, á, ü,3,4, 1,9,3].sort((a, b) => a.localeCompare(b))