0

I have some code which just does not do what is supposed to do. After inserting a lot of console.log, it finally all boiled down to an eval that made a variable undefined.

The relevant segment of code is:

console.log(str);
console.log(typeof str);
console.log(str.length);
var ret = eval(str);
console.log(ret);

and the output from Firebug-console is

[[["index.php?page=Int_JobModule&methode=Page_EditJob&ro=1&id=20121218baasbw03","20121218baasbw03","dauervorgang plumperquatsch4711x"]]]
string
135
undefined

So, clearly, eval has done this - but I can't figure out what's wrong there.

halfer
  • 19,824
  • 17
  • 99
  • 186
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • 5
    `eval` is evil! see http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea for more info – Don Jun 24 '14 at 16:07
  • 4
    Have you tried to `JSON.parse()` that? – gen_Eric Jun 24 '14 at 16:07
  • 2
    Yes do use JSON.parse. But if for some wild reason you can't - even your eval should work: http://jsfiddle.net/YuEWH/ – Yuriy Galanter Jun 24 '14 at 16:14
  • Your code works for me: http://jsfiddle.net/2EXta/ (P.S. `str.len` should be `str.length`) – gen_Eric Jun 24 '14 at 16:14
  • 1
    what are you trying to do specifically? There's probably a better way that doesn't use `eval` – Don Jun 24 '14 at 16:14
  • 1
    Your code works for me. I dont know why your are getting that undefined – Cute_Ninja Jun 24 '14 at 16:15
  • 1
    Is this literally your code? Are you sure that the `console.log` statements are lexically immediately before the `eval` and are not part of a separate function? – apsillers Jun 24 '14 at 16:18
  • In Chrome you get an object with the array on the top and an unidentified response in console. Maybe it's what is misleading you. – SimoneB Jun 24 '14 at 16:24
  • This cannot be the code you actually used, because `str.len` has to be `str.length`. –  Jun 24 '14 at 17:01
  • Thanks for all comments :) Yes, this is not the code that is actually running, because the dev-machine is not online. I had to type everything, no copy&paste. Which explains the .len/.length-mixup - I have fixed it now.However, the statements are running in exactly that sequence within the same function. – MBaas Jun 25 '14 at 05:23
  • 1
    http://stackoverflow.com/questions/7399024/how-can-i-use-js-eval-to-return-a-value – Yuriy Rozhovetskiy Jun 25 '14 at 05:48
  • Thanks @Rocket Hazmat - I have no execuse for not using JSON.parse here, it works now. If you'd re-post your comment, I'll accept is as answer :) – MBaas Jun 25 '14 at 05:54

0 Answers0