Please explain this function

This is the code from YDKJS book:

function doSomething(a) {
	b = a + doSomethingElse(a*2);
console.log(b*3)

}

function doSomethingElse(a){
return a-1
}

var b;

doSomething(2);

We get 15… I don’t understand how b is 5 here, although I see how it could be 5 but I don’t understand why JS did that, please help me.

When doSomething gets called with 2 as the argument, inside the doSomething function b gets assigned the value of 2 plus the value returned from calling doSomethingelse which gets passed the value of a (which is 2) multiplied by 2. Since a is 2, 2 *2 is 4 which gets passed to doSomethingElse. Inside doSomethingElse, the value passed to it (represented by the a local to the function) has the value 1 subtracted from it. So, since a is equal to 4, the function returns 4 - 1, which is 3. Going back to the doSomething function that 3 gets added to the a local to doSomething, so b becomes 2 + 3 (the 5) you were curious about. The value 15 gets displayed to the console because b (which is 5) multiplied by 3 is 15.

The key to understanding what is happening here, is the a parameters in each function represent completely different values passed to two completely different functions. They are local to the functions to which they belong.

1 Like

Program flow

1- 2 functions are defined
2- A variable “b” is initialized with undefined
3- doSomething is called with 2 as its argument
4- “b” is preparing to be assigned a new value
4.1- “a” is evaluated as 2
4.2 - doSomethingElse(a * 2) becomes doSomethingElse(2 * 2) becomes doSomethingElse(4) and it’s evaluated/executed with that 4 as its argument.
4.3- the execution of doSomethingElse(4) returns 3 (because 4-1 = 3)
4.4- the previously evaluated “a” (2) is added to the returned value 3 yielding a 5
4.5- that 5 is assigned to the global variable (as defined outside, with a var keyword) “b”
5- console.log(b * 3) becomes console.log(5 * 3) becomes console.log(15) and thus 15 is printed to the console; it doesn’t mean that b = 15 now, it’s just a value that you printed to the console.
6- doSomething ceases its execution and the value of b remains 5 (changed from undefined to 5 during the execution of doSomething (on the first line).


doSomething is a side-effect with two actions: assigning a new value to a global variable and printing an expressions that uses that new value.

doSomethingElse is a pure function that subtracts 1 from any number.

1 Like

LET ME JUST REARRANGE THE CODE SO THAT YOU CAN EASILY UNDERSTAND THIS

THE FIRST LINE

function doSomething(a) <–Here “a” is variable.

Last line
doSomething(2); <—Here we assigned “2” to a

Second line

b = a + doSomethingElse(a*2);

so it becomes

b = 2 + dosmthngelse(2*2);
b = 2 + dosmthngelse(4);

SECOND FUNCTION

so the second function says that
whatever dosomething else got from the first function ,return it by subtracting 1.
so,
we have dosmthngelse(4) and now subtracting 1, we got dosmthngelse(3).

NOW IT AGAIN JUMPS TO FIRST FUNCTION
so,
b = 2 + 3
b = 5
and 5*3 = 15

IMPORTANT NOTE!!!
i have not used the correct words for the syntax , like in function dosomthingelse(a) is not a variable there.
But this is just to simplify the code for you to understand.
And now you can easily read the correct answer of @RandellDawson

1 Like

Thank you very much guys, I understand it now :slight_smile: