Javascript Rest Parameters

ES6 introduced the rest parameter syntax which you should use instead of the arguments object for a javascript function. The rest parameter represents an indefinite number of arguments as a standard array. The arguments object is not an real array so methods like sort, map, forEach, or pop would not work on the arguments object, while they do on the rest parameter.

function someFunc(...args) {
  console.log(args.length);
}

someFunc();
> 0

someFunc(1,2,3,4);
> 4

// args is a valid array so this will work
function sortFn(...args) {
	return args.sort();
}

// this won't work b/c arguments isn't an array
function nonWorkingSortFn() {
	return arguments.sort();
}

sortFn('h','j','y','a','b');
> (5) ["a", "b", "h", "j", "y"]

nonWorkingSortFn('h','j','y','a','b');
> Uncaught TypeError: arguments.sort is not a function

What about this?

function sortFn(...args) {
	return args.sort();
}

sortFn(4,3,10,20,1);
> (5) [1, 10, 20, 3, 4]

🤔? This is because array.sort by default does a lexicographic (alphabetical) sort so we need to pass in our own sort function.

function sortNumFn(...args) {
    return args.sort((a, b) => a - b);
}

sortNumFn(4,3,10,20,1);
(5) [1, 3, 4, 10, 20]

😅

Read more about rest parameters here

Instagram Post