-4

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.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73

7 Answers7

0

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.

behnam shateri
  • 1,223
  • 13
  • 18
0

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);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

Undefined variables won't override existing variables.

var x = 111;
var x;
console.log(typeof x);
console.log(x);
ACD
  • 1,431
  • 1
  • 8
  • 24
0

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.

Jack
  • 5,354
  • 2
  • 29
  • 54
0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mkbctrl
  • 113
  • 6
0

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

var a;
typeof a; //"undefined"

-1

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;
Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • 2
    `var x` should implicitly assign `undefined` to `x` even if there's no intentional assignment, the problem is on the order of hoisting of functions and variables, not the assignment – Jack Apr 22 '19 at 05:44
  • @jackz314 the point is the difference between *assignment* and *declaration* Variables are **always** undefined when declared, but only if they have an **assigned** value function *declaration* won't override it. Because the order is variable declaration, function declarations then script execution which may perform variable assignment. – Kaiido Apr 22 '19 at 05:46
  • I guess we are on the same page since we both agree that the order is the problem, but the way you answered it made it sound like assignment's problem, it could be just me though. – Jack Apr 22 '19 at 05:54
  • Yes, the assignment is the problem. Since if there is no assignment then there is nothing after the function's declaration, while when there is assignment, it will get executed after. – Kaiido Apr 22 '19 at 05:56
  • I know, and this happens because of the order you mentioned, I think it's just I misinterpreted the way you parsed it, sorry. – Jack Apr 22 '19 at 06:01