Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can variable declarations always overwrite function declarations?

No matter whether I define the function after the variable

var a = 1;
function a() {};
typeof a // number

Or if I define the function before the variable

function a() {};
var a = 1;
typeof a // number

the final typeof result is always number

I found some explanation about execution context in http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/

Before executing the function code, create the execution context.
......
Scan the context for variable declarations:
If the variable name already exists in the variable object, do nothing and continue scanning.

but this does not seem to work.

So how can I explain it?

like image 506
hh54188 Avatar asked Jan 15 '14 14:01

hh54188


People also ask

How many times can you declare a variable?

a variable/function can be declared any number of times but it can be defined only once.

Why declare variables at the start of a function?

Rationale: It's best to declare variables when you first use them to ensure that they are always initialized to some valid value and that their intended use is always apparent.

What is variable declaration?

Declaration of a variable in a computer programming language is a statement used to specify the variable name and its data type. Declaration tells the compiler about the existence of an entity in the program and its location.

How do you declare a variable inside a function in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.


2 Answers

It's to do with JavaScript's variable hoisting. Try this instead:

var a = 1;
var a = function() {};
typeof a // function
like image 118
net.uk.sweet Avatar answered Oct 02 '22 22:10

net.uk.sweet


You're implicitly declaring the variable multiple times by using the function statement 'function a() {};', which as noted by others hoists the variable and behaves unexpectedly due to the order that the browser registers the declarations.

Behind the scenes, this statement instantiates a function object and assigns the result to the variable passed as the function name (reference) but this is done before the explicit var declarations are performed, and so that overrides the implicit declaration. If you just do the following, it will work more intuitively:

var a = 1;
a = function(){};
console.log(typeof a);  // function

This is a better option than the multiple var declaration in the other answer from a logical standpoint because (even though you can), it's not a good practice to declare the variable multiple times anyway.

To specifically answer the 'why' for this question: it's so that you can use these kinds of statements to define functions and use them in your explicit declarations, as in

var a = someFunction();  
function someFunction(){ return 'someVal'; }

If the function statements weren't parsed and hoisted first, this wouldn't be possible.

like image 45
jtrick Avatar answered Oct 02 '22 21:10

jtrick