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?
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?
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));