4

If I attempt to evaluate a JavaScript source block then I have to return a value to get a result. For example:

#+begin_src js
  return 1 + 2;
#+end_src

#+RESULTS:
: 3

That's a bit unusual. I guess that the code block is treated as the body of a function. Should I have to use return to get the result of this expression?

Drew
  • 75,699
  • 9
  • 109
  • 225
gburnett
  • 165
  • 7
  • 1
    https://orgmode.org/manual/Results-of-Evaluation.html suggests you have to do the same in python and some other languages. I'm not sure how to find out which languages require the `return` statement and which do not. – amitp Mar 27 '21 at 19:18
  • Thanks for the link. I followed that and this all makes perfect sense now. – gburnett Mar 28 '21 at 19:36

1 Answers1

4

For future passengers, :results value (the default option) requires an explicit return statement to display its value. On the other hand, :results output displays everything from the standard output.

Examples

In both cases, 1+2 is not enough.

#+begin_src js :results value
return 1 + 2;
#+end_src

#+RESULTS:
: 3
#+begin_src js :results output
console.log(1 + 2);
#+end_src

#+RESULTS:
: 3

Documentation

‘value’

Default for most Babel libraries. Functional mode. Org gets the value by wrapping the code in a function definition in the language of the source block. That is why when using ‘:results value’, code should execute like a function and return a value. For languages like Python, an explicit return statement is mandatory when using ‘:results value’. Result is the value returned by the last statement in the code block.

...

‘output’

Scripting mode. Org passes the code to an external process running the interpreter. Org returns the contents of the standard output stream as text results.

...

Firmin Martin
  • 1,265
  • 7
  • 23