3

My HTML has a button that runs a function that calls several other JavaScript functions. It looks like this:

<button id="ButtonSearch" onclick="FillAll()"></button>

The function FillAll() is in file Calls.js and looks like this:

function FillAll()
{
    var from_date = document.getElementById('FromDate').value;
    var to_date = document.getElementById('ToDate').value;

    FillMap();
    FillMap(from_date, to_date);
    FillRadio();
    FillArea();
}

And finally, the actual function FillMap(), which is in file Maps.js and looks like this:

function FillMap(from_date, to_date)
{   
    alert(from_date);
    var from_date = document.getElementById('FromDate').value;
    var to_date = document.getElementById('ToDate').value;
    // do stuff
}

The odd thing is that FillMap() is being called twice, and the alert pops up twice: the first one is undefined while the second one shows from_date.

Generally speaking, are JavaScript functions different to "regular" functions where the number of arguments would need to match?

In this case, the arguments don't match but the function is still being called.

2
  • 2
    @PredatorIWD Please don't use void(0). While it works, it's an ancient technique. The better choice is to simply pass null for the argument value. Commented Apr 17, 2018 at 20:16
  • JavaScript doesn't have overloads. You can only have one function/variable in a scope with the same name. Assigning another function to that name will replace the previous one instead of creating an overload. All parameters are optional in JS, you can pass more or less than the method signature and it won't complain. Commented Apr 17, 2018 at 21:04

2 Answers 2

8

Well, it's being called twice because you have two calls in your code:

FillMap();
FillMap(from_date, to_date);

For the main part of your question, in JavaScript, all arguments are essentially optional. They default to undefined unless otherwise specified:

function A(a, b, c) {
    console.log(a, b, c);
}

A(1, 2);

Strictly typed languages like Java and C# would throw a compile-time error that wouldn't let you do that.

JavaScript doesn't care and will let you call it any way.

With ECMAScript 6, you can now directly specify default values:

function A(a = 5) {
    console.log(a);
}

A();

Prior to that (and even in transpiled code now), you'd often see this pattern:

function A(a) {
    a = a || 5;
    console.log(a);
}

A();

which was a convenient way to specify the default.

Sign up to request clarification or add additional context in comments.

Comments

3

Yes, you can call a JavaScript function without passing anything into it.

JavaScript is unique in the sense that it's always just trying to 'work'., so you didn't define an argument in the first call but it implicitly passes that odd undefined argument. look up more about the undefined primitive type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.