0

I'm currently working on a site that accepts user input via ACE editor in javascript and evaluates it and displays the output in another code editor.

The problem I have is, that I can't use eval to work properly. For example, if I get the following user input:

    var i = 0;

I can call eval on the submitted String and the return value is undefined. Why don't I get 0, as the last evaluated statement should be i = 0? Another example: If I instead get the user input:

    var i = 0;
    console.log(i);

I still have an undefined return value, although I get the proper console log in my Browser console. If the user instead types in:

    i = 0;

everything works perfectly fine, the return value is zero and eval works. Is there a possibility to use eval so that it evaluates the complete code submitted like if someone would type these lines one by one into the console and get the outputs?

Thanks for any help,

rikojir

Johnny Willemsen
  • 2,942
  • 1
  • 14
  • 16
rikojir
  • 115
  • 2
  • 13
  • Possible duplicate of [Why does JavaScript variable declaration at console results in "undefined" being printed?](http://stackoverflow.com/questions/22844840/why-does-javascript-variable-declaration-at-console-results-in-undefined-being) – JJJ Mar 03 '17 at 09:51
  • Show the real code: the user-input and the eval(), because we can't help you with `var i = 0; console.log(i);` – Kulvar Mar 03 '17 at 09:55

2 Answers2

3

eval('var i = 0') doesn't print the value of i! It evaluates epressions or statements. In this case the statement var i = 0, since var i = 0 can't be true or false or any other value it is undefined

if you want to know what eval does then open the console of your browser, everything you write in is evaluate and the result is displayed the line below

Fanyo SILIADIN
  • 802
  • 5
  • 11
1

Is there a possibility to use eval so that it evaluates the complete code submitted like if someone would type these lines one by one into the console and get the outputs?

Largely, it does.

var i = 0;

I can call eval on the submitted String and the return value is undefined. Why don't I get 0, as the last evaluated statement should be i = 0?

If you type exactly that into the Chrome console (for instance), you'll see undefined, because the var statement has an empty result value. Even though in practical terms, var i; i = 0; has the same effect as var i = 0;, semantically they're different, and the latter has an empty result value whereas the former (the assignment statement) has a result value equal to the value assigned.

var i = 0;
console.log(i);

I still have an undefined return value, although I get the proper console log in my Browser console.

That's because you're seeing the return value of console.log, which is undefined.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • That makes sense, thanks. But how can I display the results of a common input like that then, without converting input that leads to a undefined return type manually? Is there a possibility to just get the printed stuff on the VM debugger / console and display that to the user? This way I could ignore the return types and simply check the things printed to the console and show them to the user. – rikojir Mar 03 '17 at 10:20
  • @rikojir: But again, if you put `var i = 0;` in the console, the output will be `undefined`; so echoing that output won't help you. As far as I know, the only way to get `0` from that is to rewrite it. That may be non-trivial, as the user can input `var i = 0, j = 42, k = function() { var n = j * 2; return n; };` or things even more convoluted than that. – T.J. Crowder Mar 03 '17 at 10:46