Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a block's return value in JavaScript

On a lot of browsers I've tested, JavaScript blocks actually return a value. You can test it out in any console:

for(var i = 0; i < 10; i++) {
    var sqrt = Math.sqrt(i);
    if(Math.floor(sqrt) === sqrt) {
        i;
    }
}

The "return" value is the last square number, that is, 9! But since it isn't an expression I suppose, you can't do this:

for(var i = 0; i < 10; i++) {
    ...
} + 5

That doesn't work. It gives + 5, or 5, of course, because it's a separate statement. Putting the loop in parentheses obviously fails, and if a block is in parentheses (e.g. ({f(); r}) - doesn't work) it's treated as an object and throws a syntax error.

One way to take advantage of the return value, as such, is to use eval:

eval('for(var i = 0; i < 10; i++) {var sqrt = Math.sqrt(i);if(Math.floor(sqrt) === sqrt) {i;}}') + 5; // 14

But I'm obviously not going to want to use that if eval is the only solution. Is there a way to use a block's resultant value without using eval that I'm missing? I really like this feature :)

like image 321
Ry- Avatar asked Dec 23 '11 16:12

Ry-


People also ask

How do you return a value from a JavaScript function?

JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the return. That value can be a constant value, a variable, or a calculation where the result of the calculation is returned.

What is the use of the return block?

If you have an error condition, just "return" out of the method. It saves wrapping the rest of your work in an else block. Seems trivial, but it helps to reduce code complexity. The compiler will actually rewrite a method to look like you described.

What is block statement in JavaScript?

The block statement is often called compound statement in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Combining statements into blocks is a common practice in JavaScript.


1 Answers

In JavaScript, statements return values of the Completion type (which is not a language type, but a specification type).

The Completion type is used to explain the behaviour of statements (break, continue, return and throw) that perform nonlocal transfers of control. Values of the Completion type are triples of the form (type, value, target), where type is one of normal, break, continue, return, or throw, value is any ECMAScript language value or empty, and target is any ECMAScript identifier or empty.

Source: http://es5.github.com/x8.html#x8.9

So, eval() evaluates the program that has been passed in as source text. That program (like any JavaScript program) returns a Completion value. The second item in this Completion value (the "value" item) is returned by the eval() invocation.

So, with eval you are able to retrieve the completion value of an JavaScript program. I am not aware of any other method to accomplish this...

like image 188
Šime Vidas Avatar answered Sep 20 '22 16:09

Šime Vidas