Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a string into an executable line of code in Javascript?

Tags:

javascript

I have the following bit of code

console.log("I am");  var x = "console.log('Alive!')"; 

Now I only want to use x to execute the code-string that is assigned to it - I may not even know the value of x for example but simply want to execute it whatever it maybe - is this possible?

like image 662
Norton Commander Avatar asked Dec 23 '12 20:12

Norton Commander


1 Answers

eval() This will convert string to javascript code.

eval("console.log('Alive! Woo!')"); 
like image 118
Simpal Kumar Avatar answered Sep 19 '22 20:09

Simpal Kumar