Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a JavaScript equivalent for C# 'params'?

Tags:

javascript

I need a method that can have an arbitrary number of parameters. In C# we have the params statement. Do we have anything similar in JavaScript?

like image 577
Kees C. Bakker Avatar asked Nov 29 '11 11:11

Kees C. Bakker


People also ask

Can I use C in JavaScript?

It is possible to implement a C API in JavaScript! This is the approach used in many of Emscripten's libraries, like SDL1 and OpenGL. You can use it to write your own APIs to call from C/C++. To do this you define the interface, decorating with extern to mark the methods in the API as external symbols.

Is there a printf in JavaScript?

The equivalent of sprintf("%. 2f", num) in JavaScript seems to be num. toFixed(2) , which formats num to 2 decimal places, with rounding (but see @ars265's comment about Math. round below).

What is statement JavaScript?

A computer program is a list of "instructions" to be "executed" by a computer. In a programming language, these programming instructions are called statements. A JavaScript program is a list of programming statements. In HTML, JavaScript programs are executed by the web browser.

How do you print a string in JavaScript?

JavaScript Print JavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is that you can call the window.print() method in the browser to print the content of the current window.


5 Answers

There is the arguments collection, which contains all arguments passed to the function.

There is a) no need to specify "optional" arguments in the function signature and b) any function accepts any number of parameters.

function foo() {
  console.log(arguments);
}

foo(1,2,3,4);  // logs [1, 2, 3, 4]

Likewise, there is no need to supply "required" arguments in a function call:

function foo(a, b, c, d) {
  console.log(arguments);
}

foo(1,2);  // logs [1, 2]

Any argument named in the signature but not supplied in the function call will be undefined.

Note that arguments behaves like an Array, but technically it isn't one. For example, you can call arguments[0], but you can't call arguments.slice(). What you can do to get around this is using the Array prototype:

Array.prototype.slice.call(arguments, 1, 2);

The so-called rest parameter ... is a new (ES6+) addition to the language and makes working with variadic functions more comfortable. @ArunCM's answer explains it.

like image 169
Tomalak Avatar answered Oct 03 '22 02:10

Tomalak


I know this thread is too old but I believe something is missing here.

There is Rest parameter (introduced in ECMAScript 6) which will allow us to represent an indefinite number of arguments as an array.

It always returns an array. Which means even in defensive JavaScript land, it’s ok to do things like check .length of rest without guards.

Syntax :

function(a, b, ...theArgs) {
// ...
}

There are three main differences between rest parameters and the arguments object:

  1. rest parameters are only the ones that haven't been given a separate name, while the arguments object contains all arguments passed to the function
  2. the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
  3. the arguments object has additional functionality specific to itself (like the callee property).

Additional reading : Spread

function f(x, ...y) {
  // y is an Array
  return x * y.length;
}

console.log("Expected result : 3*2 = 6 & Actual result : " + f(3, "hello", true));
console.log("Expected result : 3*4 = 12 & Actual result : " + f(3, "a", true, "b", 1));

//here we are not passing anything to "y" but its still safe to check .length of "y" because it always return an array.
console.log("Expected result : 3*0 = 0 & Actual result : " + f(3));
like image 39
Arun CM Avatar answered Oct 03 '22 02:10

Arun CM


Yes. arguments.

function concatStrings () {
    var str = '';
    for (var i = 0; i < arguments.length; i++) {
        str += arguments[i];
    }
    return str;
}

Be aware that arguments isn't an array, so it doesn't have methods like join or push. It's just an array-like object (with numerical properties and a length property) so it can be iterated through.

like image 39
Nathan MacInnes Avatar answered Oct 03 '22 02:10

Nathan MacInnes


It is some sort of implicit in the special variable "arguments". Use like this:

function something(arg1, arg2) {
    for (var i = 0; i < arguments.length; i++) {
        var x = arguments[i];
    }
}

Then you can call it like something(1, 2, 3, 'a', 'b', 'c')

More examples here: http://www.jtricks.com/javascript_tutorials/varargs.html

like image 34
Rafael Steil Avatar answered Oct 03 '22 03:10

Rafael Steil


JavaScript has arguments object inside functions. It contains of all params passed to the function.

More info

like image 33
antyrat Avatar answered Oct 03 '22 02:10

antyrat