Return problems


#1


https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/1/2?curriculum_id=506324b3a7dffd00020bf661


This piece of code will return:

[Function]
16

The code itself works (*2), but it also returns [Function]
Can anyone tell me why this is?


// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
    return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo;
console.log(newNumber);
timesTwo(8);


#2

Hi there,

At the moment you're assigning the just code structure of the function timesTwo to newNumber.
When you do this you're not executing the function in any way. That's why when you console.log(newNumber) it shows the string representation of the function. If you were to instead do console.log(newNumber(8)) then you would be calling the function. Big difference whether you include the parentheses or not.

However with that being said I think you're meant to do:

var newNumber = timesTwo(8);

Doing it this way will avoid the unnecessary transferal of code sructure from one variable to another and instead just call the timesTwo function straight away and assign the return value to newNumber.

Hope that makes sense.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.