0

In this code snippet, trueFactorial(5) returns 120:

function factorial(num) {
  if (num <= 1) {
    return 1;
  } else {
    return num * arguments.callee(num - 1);
  }
}

var trueFactorial = factorial;

var factorial = function(num) {
  return 0;
}

console.log(trueFactorial(5));

But in this code snippet, trueFactorial(5) returns 0.

function factorial(num) {
  if (num <= 1) {
    return 1;
  } else {
    return num * arguments.callee(num - 1);
  }
}

var trueFactorial = factorial;

function factorial(num) {
  return 0;
}

console.log(trueFactorial(5));

The only difference is that we declared factorial through variable assignment in the first snippet. What is the reason for this difference?

3
  • Learn about hoisting Commented Sep 21, 2017 at 12:18
  • @LarsPeterson — The difference is on line 11 and explained in the question title. Commented Sep 21, 2017 at 12:18
  • @Quentin I see it now Commented Sep 21, 2017 at 12:19

3 Answers 3

2

Function declarations are hoisted. Variable assignments are not.

In example one, you assign the first function to trueFactorial.

In example two, the second function is hoisted so it gets assigned to trueFactorial.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I had a feeling it would be that. So is it correct to say that in function hoisting, the last function body is always the one that will be assigned to the variable function name?
For a given scope, yes.
0

The JavaScript interpreter first looks at the declaration of all functions in your code with the function <...> (<arguments>) syntax. In your second snippet, you declare the function both time with this syntax, so the interpreter starts by seeing the first declaration, memorize its content, and then see another declaration. At this point, it will replace the first declaration by the new one.

When you use the var keyword, the declaration is not seen by the interpreter before the start, but while the code is running.

5 Comments

This has nothing to do with the var keyword. The declarations in both cases are the same. It's the assignments that have different timing.
Yes it does, because var is an assignment that changes the function's content during the run, while the function <...>(<arguments>) syntax changes the function's content before the program starts.
You are confusing the significance of var (which declares variables) and =(which assigns values to variables). (A function declaration will both declare a variable and assign a value to it).
Yes I didn't explain well, but a function declaration doesn't just declare and assign a value to a variable but declare even before the programs starts what is the function's content.
var is also hoisted and causes the variable to be declared throughout the entire function.
-1

In the second example your function is hoisted and gets assigned to trueFactorial function. Meaning it is in the end a reference to the same function. In the first example the reference points to another address in memory.

See:

function factorial(num){
    if (num <=1){
        return 1;
    } else {
        return num*arguments.callee(num-1);
    }
}

var trueFactorial = factorial;

var factorial = function(num){
    return 0;
}

console.log(trueFactorial(5));//120

console.log(trueFactorial === factorial);//false

function factorial(num){
    if (num <=1){
        return 1;
    } else {
        return num*arguments.callee(num-1);
    }
}

var trueFactorial = factorial;

function factorial(num){
    return 0;
}

console.log(trueFactorial(5));//0

console.log(trueFactorial === factorial);//true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.