Return Keyword - Substituting 'function()' with the name of a variable


#1

Lines 2-4:

var timesTwo = function(number) {
return number * 2;
};

Lines 7-8:

var newNumber = timesTwo(8);
console.log(newNumber);


I had trouble with this exercise. I was trying to rewrite lines 7-8 the same way I was writing lines 2-4, with a function:

var newNumber = function(number)
..... but we are supposed to substitute the 'function(number)' with the name of the first variable, defined on line 2....
var newNumber = timesTwo(8)

I'm actually still kind of confused. The exercise is supposed to be about using the return function. Learning to sub out a 'function()' with the name of a previously defined variable should be it's own lesson.


#2

return is not a function, it's an instruction to exit the current function.

I'm not sure what you mean by substitution, perhaps that equivalent expressions can substitute each other?


#3

Hello @nmhicks,

On lines 2-4 you have assigned a function that takes one parameter, number, to the variable timesTwo. Just like when you assign a value of an int or a string to a variable, to call the variable you have to use the variable's name, not function(number). And as @ionatan said, return isn't a function, it is a keyword that sends that value out whenever the function is called.

Hope this has cleared out your understanding

Regards,
Barry Allen


#4

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

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