Answers for "How can I convert the "arguments" object to an array in JavaScript?"

0

How can I convert the "arguments" object to an array in JavaScript?

//ES6 using rest parameters
function sortArgs(...args) {
  return args.sort(function (a, b) { return a - b; });
}
document.body.innerHTML = sortArgs(12, 4, 6, 8).toString();


//ES6 using Array.from()
function sortArgs() {
  return Array.from(arguments).sort(function (a, b) { return a - b; });
}
document.body.innerHTML = sortArgs(12, 4, 6, 8).toString();


//ES5
function sortArgs() {
    var args = Array.prototype.slice.call(arguments);
    return args.sort();
}
Posted by: Guest on September-25-2021

Code answers related to "How can I convert the "arguments" object to an array in JavaScript?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language