Being new to javascript, I have a question. Lets, consider a string that looks like this :
var str = "Math.random() > 0.5";
Now lets have some javascript :
console.log( turnUp( str ) ); // true or false
So, turnUp() is just a function I assumed can do something like this :
Without turnUp() :
console.log( str );
// Is equivalent to console.log( "Math.random() > 0.5" );
Output :
Math.random() > 0.5
With turnUp() :
console.log( turnUp( str ) );
// Is equivalent to console.log( Math.random() > 0.5 );
Output :
true
Or
Output :
false
So, with the help of examples, you might understand what I need ! So, how to make the turnUp() function ?
Thanks In Advance
eval() has many drawbacks so I prefer you to do something like this :
var turnUp = function(str) {
return Function(' "use strict"; return (' + str + ') ')();
}
Now you can do :
var str = "Math.random() > 0.5";
console.log( str ); // => Math.random > 0.5
console.log( turnUp( str ) ); // => true or false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With