Q:

when the alert() in string , how to convert it as a function to show the alert in javascript

When an alert() method is in double quotes it  behaves like a string, meaning when it is printed in the console, the console screen shows the alert(). But i want, when there is an alert() in console.log(), to show an alert box. I have written the following piece of code but it does not fulfill my requirements:
var msg="alert("welcome")"
console.log(msg)

When I run the above code, the output to the console is alert("welcome") and no alert box is created. Could anyone help me figure out how to show an alert box?
Thanks

A:

If you want to do two things, you need code to do two things. If you want to do two things with one statement, you need to make a function that does both things for you.
function doubleAlert(msg) {
  console.log(msg);
  alert(msg);
}

A:

You should use the eval() function:

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.

var msg = "alert(\"welcome\")"; // Sample message.
console.log(msg);             // Log to the console regardless.
eval(msg);                    // Evaluate the message as an expression.
// Alternatively, regarding your comment about the message being altered, you can do one of the following:
var msg = "alert('welcome')";
console.log(msg);
eval(msg); 
// Or:
var msg = 'alert("welcome")';
console.log(msg);
eval(msg); 
// Or even:
var msg = 'alert(\'welcome\')';
console.log(msg);
eval(msg); 

