0

So in Java, you can do:

void F(int x, int y) {
  run(x, y);
}

void F(String a) {
  say(a);
}

but in Javascript you always have to do:

function F(x, y) {
  if (typeof x == "string") {
    say(a);
  } else {
    run(x, y)
  }
}

I want to do something like the Java functions in Javascript without checking every single thing, anyway to do that?

1 Answers1

2

You can check the number of arguments using the arguments variable, like this:

function F(x, y) {
  if (arguments.length == 1) {
    say(x);
  } else if (arguments.length == 2) {
    run(x, y)
  }
}
William Barbosa
  • 4,936
  • 2
  • 19
  • 37