1

I have confusion on function calling in javascript,Can some one tell answer for below question ?

**Question 1:**
function A(){


}


function A(){


}

A();

In this case,which function will call?is it first or second function? and why?

**Question 2:**
function A(a){


}


function A(a,b){


}

A();

In this case,which function will call?is it first or second function? and why?

Thanks in advance.

4
  • 3
    Second function for both because it is declared and defined after the first declaration. Commented Sep 26, 2016 at 5:55
  • you can not override a function in this way , javascript call function using function name , nothing to do with parameters . Commented Sep 26, 2016 at 6:00
  • There are several duplicates of this type of question. Commented Sep 26, 2016 at 6:02
  • In cases of naming conflict like this, the first declaration defines the symbolic name and each subsequent one overrides and assigns a new function to that symbol. Commented Sep 26, 2016 at 6:03

4 Answers 4

5

Let's put it in action and see the results:

function A(){
  var el = document.getElementById('test');
  el.innerText  = "First function";
}

function A(){
  var el = document.getElementById('test');
  el.innerText  = "Second function";
}

A();
<div id="test"></div>

As we can see, the second function is the winner. Why? Because when we write it again after a first declaration we just overwrite it really.

As for the second question:

function A(a){
  var el = document.getElementById('test');
  el.innerText  = "First function";
}

function A(a,b){
  var el = document.getElementById('test');
  el.innerText  = "Second function";
}

A();
<div id="test"></div>

As we see, it still executes the second question. That's because javascript isn't a polymorphic language, that is, it cannot have two function with the same name but with different input declarations like java does. We just did the same thing we did above: We overwrite the function A.

PS: If javascript were a polymorphic language (which it isn't), then the second run would return an error because we haven't declared an version of function A that receive 0 variables as input.

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

Comments

2

Second function will be called in both the cases, as the your redefining the function definition.

Also there is no function overloading in javascript, in the second case change function signature( i.e arguments it takes) will redefine the same A function (defined 1st).

Comments

1

When a parser enters the script, it searches for var statements and function declarations and then creates those variables on the current scope (in your case it's a global scope). This process of searching and creating is called hoisting. So in your case when parsing the first function declaration is found. So, something like that happens:

scope.A = function A(){} // first function

Then parser continues searching and finds another function declaration. And the same happens:

scope.A = function A(){} // second function

As you can see, scope.A now references second function.

Comments

0

In both cases the second function will be called because it overwrites the global variable A when declared.

It's similar to writing:

var a = 1;
var a = 2;

a === 2; // true

2 Comments

This is a bad analogy because function declarations declare and define at the same time (hoisted), however variable declarations behave differently where it only declares it but not define it. (Demo for readers in the future)
That's why I write that it's similar, not the same.

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.