Javascript return in eval string

javascript eval alternative
javascript string to function without eval
eval javascript
javascript safe eval
javascript eval function string
eval in javascript security risk
javascript eval string to object
eval can be harmful

I know eval is evil, but in this case please do not point this out please. I am creating something very custom and in my case eval is the only solution (that is what eval is there for).

My problem is that return does not work in eval strings. The browser throws and Illegal return statement error. Could someone explain to me why this happens?

I would like this string to be eval'ed:

eval(`
  console.log('log this');
  return;
  console.log('DONT log this')
`);

Here is a stack blitz to play with

https://stackblitz.com/edit/angular-8x8p6m?file=src%2Fapp%2Fapp.component.ts


Try inputting console.log('log this'); return; console.log('DONT log this'); in your browser (that's basically what eval is); it'll complain because you're trying to use return outside of a function.

However, you can do something like this:

eval(`
  (() => {
    console.log('log this');
    return;
    console.log('DONT log this');
  })()
`);

How can I use js eval to return a value?, If code contains a JavaScript statement or statements, eval( ) executes those statements and returns the value, if any, returned by the last statement. If code does  eval(string) Parameters string A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. Return value. The completion value of evaluating the given code. If the completion value is empty, undefined is returned. Description


This is why we use new Function() instead of eval() if we really need eval() for something.

If you pass a string to the Function constructor, it will automatically call eval() on it. So no need to wrap eval And function in the same statement, that's basically doing the same thing twice.

const js_str = `
  console.log('log this');
  return 'ok';
  console.log('DONT log this')
`;

const result = new Function( js_str )();

console.log( result );

eval( ): execute JavaScript code from a string, JavaScript eval() Function Evaluate/Execute JavaScript code/expressions: string, A JavaScript expression, variable, statement, or sequence of statements  The eval() function evaluates or executes an argument. If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.


Your problem is that you're not in a function:

eval('(new Function("console.log(1);return;console.log(2)"))()');

Logs 1 but not 2.

JavaScript eval() Function, eval(). eval(str) evaluates the JavaScript code in str. For example: > function strictFunc() { 'use strict'; var code = '(function () { return this }())'; var  Return a 'string' from javascript eval() function. Is there any way to execute and return string out of eval Browse other questions tagged string eval or ask


As mentioned in the comments, return statements are only valid inside functions

Give this a try:

eval('(function (){ 
  console.log("log this");
  return 23;
  console.log("do not log this"); 
})()');

Evaluating JavaScript code via eval() and new Function(), A call to eval(code) runs the string of code and returns the result of the last statement. Rarely used in modern JavaScript, as there's usually no  eval() is a global function in JavaScript that evaluates a specified string as JavaScript code and executes it. Example: eval eval( "alert('this is executed by eval()')" );


Eval: run a code string, If the completion value is empty, undefined is returned. Examples: Input : eval(​new String('2 + 2')); Output  eval() The eval function in it’s default form looks like this:. eval ("alert('lorem ipsum')");. This will parse and execute code provided as a string in current context. It even allows you to define variables within current scope which leads to many performance and security problems.


Javascript, eval() is a global function in JavaScript that evaluates a specified string as var result; function Sum(val1, val2) { return val1 + val2; } eval("result = Sum(5, 5);");  eval is evil in JavaScript! The MDN eval page states: Obsolete This feature is obsolete. Although it is still supported by browsers, its usage is discouraged in new projects. Try to avoid using it.


eval in JavaScript, is stored in a string that is given as an argument to the built-in function eval. Through a call to eval a JSON file can directly become a JavaScript object. the test function usefun() will call this function and show the value it returns based on​  To return a string from a JavaScript function, use the return statement in JavaScript.