-4

How I can to define array parameter in javascript function. For example in c# I can just like this.

private void myFunc(params object[] values){

}
Adil
  • 124
  • 7

2 Answers2

1

JavaScript is dynamically typed. Any argument can be passed an array. There is no special syntax to declare it.

function example(my_argument) {
    if (my_argument instanceof Array) {
       console.log(my_argument, "is an array");
    } else {
       console.log(my_argument, "is not an array");
    }
}

example([1,2,3]);
example("a string");
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You don't specify type of argument in Javascript, it's dynamically typed language. In case you want to check explicitly type of argument then you can do something like:

function(arg1) {
    if (typeof arg1 !== 'object') {
        throw new Error('Invalid type of argument');
    }
}

Also take a look at this answer.

real
  • 11
  • 3