What is the output of following code?
var b = 20;
eval('var b = 10');
console.log(b);
----o----
The output is 10
.
eval()
evaluates the passed argument string as an expression. Here, the expression is changing the value of b
from 20 to 10. That is why the output prints 10
.
Since eval()
can dynamically change the scope and flow of code, it is advised NOT to use it whenever possible.