Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when JavaScript variable name and function name is the same?

I have the following code, where I declare a function and after it, a variable with the same name as the function:

function a(x) {
    return x * 2;
}

var a;
alert(a);

I expected this to alert undefined, but if I run it, the alert will display the following:

function a(x) {
    return x * 2
}

If I assign a value to the variable (like var a = 4), the alert will display that value (4), but without this change a will be recognized as a function.

Why is this happening?

like image 243
joesid Avatar asked Nov 18 '16 11:11

joesid


People also ask

Can function name and variable name be same?

Variables and functions have separate name spaces, which means a variable and a function can have the same name, such as value and value(), and Mata will not confuse them.

What happens if we have two functions with same name in JavaScript?

JavaScript supports overriding not overloading, meaning, that if you define two functions with the same name, the last one defined will override the previously defined version and every time a call will be made to the function, the last defined one will get executed.

Can you have two variables with the same name JavaScript?

It's not possible, an if statement has no special scope, so you can't have two variables with the same name within the same scope and access both, the latter will overwrite the former, so they should have different names.

Can the same variable name be used in more than one function?

Yes you can, as long as you don't forget the var keyword : the scope of a variable is either the function in which it is declared or the global scope.


4 Answers

Functions are a type of object which are a type of value.

Values can be stored in variables (and properties, and passed as arguments to functions, etc).

A function declaration:

  • Creates a named function
  • Creates a variable in the current scope with the same name as the function (unless such a variable already exists)
  • Assigns the function to that variable
  • Is hoisted

A var statement:

  • Creates a variable in the current scope with the specified name (unless such a variable already exists)
  • Is hoisted
  • Doesn't assign a value to that variable (unless combined with an assignment operator)

Both your declaration and var statement are hoisted. Only one of them assigns a value to the variable a.

like image 187
Quentin Avatar answered Oct 02 '22 04:10

Quentin


In JavaScript both function declaration and variable declarations are hoisted to the top of the function, if defined in a function, or the top of the global context, if outside a function. And function declaration takes precedence over variable declarations (but not over variable assignment).

Function Declaration Overrides Variable Declaration When Hoisted

First you declare a variable:

var a; // value of a is undefined 

Second, the value of a is a function because function declaration takes precedence over variable declarations (but not over variable assignment):

function a(x) {
  return x * 2;
}

And that is what you get when you call alert(a);.

But, if instead of declaring a variable you make variable assignment: var a = 4; then the assigned value 4 will prevail.

like image 26
Yosvel Quintero Arguelles Avatar answered Oct 02 '22 05:10

Yosvel Quintero Arguelles


If you use a function name as variable name, its value is replaced by function body. So var a becomes your function a and thus your alert displays function a.

Edit But if you assign value to a like var a = "xya";. Then it function will be replaced by variable. As per Order of precedence

  1. Variable assignment takes precedence over function declaration
  2. Function declarations take precedence over variable declarations
like image 4
Rahul Agrawal Avatar answered Oct 02 '22 05:10

Rahul Agrawal


You should also remember that var a is hoisted, which makes it more like this

var a; // placed

function a(x) {
  return x * 2;
};

var a; // removed
alert (a); // a is replaced by function body

Remember that var a is hoisted, so if you assign 4 to a:

var a; // placed

function a(x) {
  return x * 2;
};

var a = 4; // removed
a = 4 // added

alert (a); // a is now 4
like image 2
Akinjide Avatar answered Oct 02 '22 05:10

Akinjide