Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will the reference be when a variable and function have the same name?

Tags:

javascript

I have a variable named foo and function named foo.

//variable followed by function declaration
var foo="bar";
function foo(){
  return "bar";
}
//function declaration followed by variable
function foo(){
  return "bar";
}
var foo="bar";
//When I call foo it returns string bar;
//When I enquired foo() it throws error

What's happening here?Why does the variable name override function declaration?

like image 620
krtkeyan Avatar asked Oct 17 '16 03:10

krtkeyan


People also ask

Can a variable name and function name be same?

You cant because if you have example(), 'example' is a pointer to that function. This is the right answer. There are function pointers, which are variables. But also, all function names are treated as const function pointers!

What is the preference given if a function and variable has same name?

If you use a function name as variable name , its value is replaced by function body .

What will happen if you declare a variable in a function with same name as a global variable?

If a global and a local variable with the same name are in scope, which means accessible, at the same time, your code can access only the local variable.

Can variables have the same name?

In a word, yes. Variable names only hold in the scope they're defined in, and you can use the same name in different scopes.


3 Answers

When I call foo it returns string bar;

Function declarations are hoisted to the top of their scope. The function definition are moved above by the compiler. And then the variable is overwriting to string.

Code is equivalent as

function foo() {
    return "bar";
}
// Overwriting the value
var foo = "bar"

So, in both the cases, the output will be 'bar'.

Note that function expressions are not hoisted.

For more information on function hoisting, see Javascript function scoping and hoisting

When I enquired foo() it is undefined

foo here is not a function, it's a string. So, foo() will throw an error

Uncaught TypeError: a is not a function(…)

like image 90
Tushar Avatar answered Sep 18 '22 16:09

Tushar


In a clearer and more explicit way of declaring variables, the latter will take account:

    var foo = "bar";
    var foo = function () {
        return "bar";
    };
    console.log(foo);

output is a function

and the reversal:

var foo = function () {
    return "bar";
};
var foo = "bar";
console.log(foo);

has "bar" as output.

like image 31
Bob Dust Avatar answered Sep 18 '22 16:09

Bob Dust


In JavaScript, functions are processed upon entering the corresponding scope. The variables are processed when the interpreter gets to their declaration. Therefore in your example, the functions are processed first, the name foo is used by the last function and then overwritten by the variables.

Note that if you declare your function like this

var foo = function() {}

it is actually not processed at the beginning and also overwriting the variables declared beforehand.

like image 37
mxscho Avatar answered Sep 18 '22 16:09

mxscho