Closed remal closed 1 year ago
Just remove the return.
But what if I have a much more complex code with conditions?
The value from the last expression is returned. You could also use a function and immediately call it.
(function(){
return something;
})()
@XmiliaH it works, thanks! The issue can be closed.
I think it would be great if this scenario were documented somewhere. What do you think?
I would argue that it already is documented. From the README:
You can also retrieve values from VM.
let number = vm.run('1337'); // returns 1337
But feel free do make a PR.
@XmiliaH you're absolutely right - it is documented. However, it's a bit confusing why return
doesn't work.
Since the JavaScript top-level scope cannot use the return
statement. It is the same for the node vm
module.
This code fails with
SyntaxError: Illegal return statement
error. How can it be fixed/rewritten?