Hey! recursion makes use of the stack data structure to keep track of the function that is currently running as well as variables associated with that function. the functions on top of the stack are run first.
Recursive functions basically keep pushing functions on to the stack until a certain condition is met, in your case that condition is if num === 0. You can think of it as saying.
“I dont care what the factorial of n is, first find the factorial of num - 1 and keep repeating it until n === 0”
the first function’s execution starts, at this point the value of num is 5 which is greater than 0 so this code will run.
5 * factorialze( 5 - 1)
now the function
factorialze( 5 - 1)
is pushed to the stack which means this function’s execution will start and the outer function’s execution ( factorialize(5) ) will halt until it finds the result of factorialize(4).
in factorialize(4) the value of num is 4 which is greater than 0 then this code will run
4 * factorialze( 4 - 1)
and at this point factorialize( 3 ) will be pushed to the stack and the the execution can not go any further until it finds the result of factorialize(3).
and this will keep going until the condition ( num === 0 ) is met.
At which point, we return 1. this starts a backwards effect in which the value returned from one function is used to finish the execution of the function lower in the stack.
when the condition is met, we have something like this,
5 * fac( 4 ) * fac( 3 ) * fac (2) * fac( 1 ) * fac( 0 )
here fac( 0 ) resolves to 1 so we can subsitute it here and this becomes,
5 * fac( 4 ) * fac( 3 ) * fac (2) * fac( 1 ) * 1
now if we have fac of 0, we can calculate fac(1) because it is just
1 * fac( 0 )
// which we can also write as
1 * 1
// now we get this
5 * fac( 4 ) * fac( 3 ) * fac (2) * 1 * 1
// finding out the value of fac( 2 ) is just as easy
// because it requires us to substitute the value of fac(1)
5 * fac( 4 ) * fac( 3 ) * ( 2 * 1) * 1 * 1
5 * fac( 4 ) * fac( 3 ) * 2 * 1 * 1
After returning a value, each function is pushed off of the stack and the value returned from it is used to calculate the function lower in the stack until the final result becomes
5 * 4 * 3 * 2 * 1 * 1
which is how you calculate the factorial of a number.
if you want to learn more about recursion in general, i would recommend you to check out this series
Hope this helped!