-2
console.log(Math.sqrt(4));
let str = 'Math.sqrt(4)';
console.log(str);

Is there any way for the 2nd console.log to show 4? Is there any function which can do this?

Phil
  • 157,677
  • 23
  • 242
  • 245
misha
  • 1
  • Last I checked the square root of 4 is 2. Also: [`eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) is evil. – Robby Cornelissen Jun 30 '22 at 06:25

2 Answers2

0

You can use eval(), like

let str = eval('Math.sqrt(4)');
0

To do this, you can use the eval() function to evaluate the code in the string. Like this:

let str = 'Math.sqrt(4)';
console.log(eval(str));