Search code examples
javascriptfunctionecmascript-5

JavaScript: Print argument received by function


In Javascript ES5, is there any in-built object or array where arguments received by a function ? My need is, I have a function which improved recently but I want to add backlog support as well for other classes which are using this old API.

So if no arguments passed, I execute one block of code and with arguments, other block.

Any suggestions/help much appreciated.


Solution

  • Yes, you may use the arguments keyword, for example,

    function f() {
      return arguments.length
    }
    
    console.log(f(1)) // 1
    console.log(f(1,2)) // 2
    
    

    The arguments keyword-variable can be treated as normal JavaScript arrays.

    For more information, please refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments