javascript - Does the return statement work from inner to outter? -


I am struggling with the return statement in JavaScript. Here is a function that I have written that converts a binary number into a decimal number:

  function bin2dec (num) {** return ** Num.toString () .split (''). Reverse (). Low (function (prevalence, running, itater) {** returns ** (currentvalue === '1')? Prewail + math.pau (2, iterator): prevalence;}, 0); }  

Use Binary 5 as an example of working with Binari 5 = 101

, so I thought that whenever you return the statement , It exits the current function from time to time.

Here are two return statements, inside a callback function.

The first return statement is on the second row:

  back to the number. Toasting () the division (''). Reverse () // and then decrease the function w / a call back.  

I believe this 'return' statement is waiting to reduce the return of the function function

The function takes the number, making it a string , Then divides, then reverses, then decreases the method over the array. When the function is reduced, it returns a value, which is then returned by the return value first. Here's more information about reducing function return value.

In the callback:

  // 101.reduce (function (prevalence, running, iterator) {return (= current === '1')? Prewail + monastery Pow (2, iterator): prevalence;}, 0);  

It runs on each part of the array, in this case the 'return' is prevalent until the current value === 1. The 'return' value is passed to the callback and the value of the prevalence for each loop, we are adding and constructing the current valve from the earlier vall. When everything in the array is turned on, the return statement is the last value. The final value is then returned by 1 return statement which has not yet been executed.

I am actually lost from the return statement. I am mainly trying to check how I am describing the 'Return' statement.

  1. For a quick referrance

    First return - waiting for the evaluation of everything, so there is no object to return.

  2. The second return - is used for the callback and when it is repeated, the last return value is returned, then the value from the first return statement Which is ultimately out of the function.

Let's see

  Return [no linear terminator] expression;  

and it is evaluated as

if expression is omitted, the return value is undefined . Otherwise, the return value is the value of expression .

You can see, there are no words about other return statements. Any expression is valid in a return statement and does not make any difference to it what expression is actually.


The follow-up question occurs when num.toString (). the division (''). Reverse (). Reduce (...) and how it is evaluated.

num ToString (). the division (''). Reverse (). Reduce (...) is an call expression and the result of this expression is the return value of the function.

How do lower work? Reduce converts an array into a single value. It does so that the return value of that function by executing a function for each element in the array and the previous move is passed in the next move.


So the bottom line is that there is nothing to do with the two returns statements, they are completely free, it is easy to see Can you already define the function:

  function bin2dec (num) {function callback (prevalence, running, iterator) {return (current === '1') ? Preval + Matho Pove (2, Iterator): Prevalence; } Return num.toString (). Split (''). Reverse (). Low (callback, 0); }  

Comments