Consider the two following snippets

function x(){}
var x = 3;
console.log(typeof x) //"number"

This happens because function declarations are hoisted first.

function x(){}
var x;
console.log(typeof x) //"function"

I expect the type to be undefined because var x; creates a variable with undefined value.

7 Answers

-1
Kaiido On Best Solutions

var x declares the variable xbut doesn't assign anything to it.

If you did set its value to undefined, then that would be undefined:

function x(){}
var x = undefined;
console.log(typeof x) //"undefined"

So what you made in your example is just

var x;
function x(){}

While in my example it becomes

var x;
function x(){}
x = undefined;
0
behnam shateri On

its because of that in first section you set x as new variable with type of number,in another words, var x=3 is override on x and has type of number because of 3 but in second section, you just call x, and say it is variable that has type of function.

0
Jack Bashford On

It's because in JavaScript, if a function and a variable with the same name and scope are declared, they are hoisted like so:

If the variable has a value, or gets a value at any point, it is hoisted to the top and takes precedence.
If the variable is created but has an undefined value (e.g. var x;), then the function takes precedence.

This does not happen when you explicitly set a value to undefined, like var x = undefined

That's why this happens (it gets a value at any point)

function x() {}
var x;
x = 4;
console.log(typeof x);

Same here - even with odd variable and function placement, because the variable gets the value:

var x;
function x() {}
x = 5;
console.log(typeof x);

0
ACD On

Undefined variables won't override existing variables.

var x = 111;
var x;
console.log(typeof x);
console.log(x);

0
jackz314 On

The rules for hoisting are:

  1. Variable assignment takes precedence over function declaration
  2. Function declarations take precedence over variable declarations

So the order of the hoisting would be

Variable declaration -> Function declaration -> Variable assignment

Take a look at this article, basically, to quote the article, the reason this is happening is because:

Function declarations are hoisted over variable declarations but not over variable assignments.

In this section, it even has the exact same example as you gave in your question.

To sum up, your declaration of function x can't "overwrite" (hoist over) your variable assignment, but can "overwrite" your variable declaration.

0
Community On

The x returns the type of function, because

"In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects."

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

Basically you have created 2 objects referencing the same name x and because var x is not given any kind of value, the function is returned as it is taking precedence over unassigned variable.

0
Community On

In JavaScript, an undeclared variable is assigned the value undefined at execution and is also of type undefined.

var a;
typeof a; //"undefined"