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){
}
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){
}
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");
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.